Page 1 of 1

What API calls to add upload option?

Posted: Tue Sep 25, 2007 2:57 pm
by Skruu
Can anyone point me in the right direction concerning the following:

I would like to add an upload option to my component in the backend to upload a picture. I would like to do it in a good 1.5 way.
Does anyone have a piece of example code with the right API calls, so I can study it and take the code as a base?

Re: What API calls to add upload option?

Posted: Tue Sep 25, 2007 6:26 pm
by ianmac
I would take a look at the media manager in the backend.

Ian

Re: What API calls to add upload option?

Posted: Tue Oct 02, 2007 11:30 am
by zian974
Hi,

I made this to upload txt file for my component. this is the function i wrote, free inspirate by './administrator/components/com_installer/models/install.php'. I think it work for other type of file (png, jpg, ...).

To manipulate File and folder, you need to import JFile and JFolder class.
I use JRequest::getVar with the attribute 'files'.

Code: Select all

public function upload(  )
{
   jimport('joomla.filesystem.*');
      
   // Récupération du fichier
   $userfile = JRequest::getVar('fileEsp', null, 'files', 'array' );
      
   // Check if there was a problem uploading the file.
   if ( $userfile['error'] || $userfile['size'] < 1 )
   {
      JError::raiseWarning('SOME_ERROR_CODE',  'Erreur de transfert du fichier' );
      return false;
   }

   // Build the appropriate paths
   $tmp_dest = PATH_TXTUPL.DS.'especes.txt';
   $tmp_src   = $userfile['tmp_name'];
      
   // Write the file $tmp_src in $tmp_dest folder
   if ( JFile::upload($tmp_src, $tmp_dest ) )
   {
      return true;
   }
      else
      {
         return false;   
      }
  }


And the form

           enctype="multipart/form-data" method="post"
           action="">

         

A partir du fichier .txt



                   
         


         



I hope this will help you.

Na retrouvé.

Re: What API calls to add upload option?

Posted: Tue Oct 02, 2007 10:30 pm
by ghostrifle
Well... I tried your example code but I doesn't work 'cause the task doesn't get called.

I implemented the upload-function in my controller where I edit, add and display all other single entries of my component.

Changing the input string of JRoute to match with my component doesn't work also:

Code: Select all

<form   class="formEsp" name="upL_txtEsp" id="upL_txtEsp" enctype="multipart/form-data" method="post"action="<?php echo JRoute::_( 'index.php?option=com_redlight&controller=redlight&task=upload&id='.$this->rl_ad->id ) ?>">
            <fieldset>
         <legend>Anzeigenfoto hochladen:</legend>  
         <p>
            <label for="fileEsp" title="Wähle ein Foto aus">Foto</label>
                <input type="file" name="fileEsp" id="fileEsp" title="Wähle ein Foto aus" size="50"/>
            <input type="submit" value="hinzufügen" size="50"/>
         </p>  
          </fieldset>  
      </form>


Well... here'ss my editied code from the controller. If the method would get called, I would either get a "noooo" or "yeeeees" message:

Code: Select all

function upload()
   {
      jimport('joomla.filesystem.*');
      
      // Récupération du fichier
      $userfile = JRequest::getVar('fileEsp', null, 'files', 'array' );
      
      // Check if there was a problem uploading the file.
      if ( $userfile['error'] || $userfile['size'] < 1 )
      {
         JError::raiseWarning('SOME_ERROR_CODE',  'noooooo' );
         return false;
      }
      else JError::raiseWarning('SOME_ERROR_CODE',  'yeeeees' );

      // Build the appropriate paths
      $tmp_dest = PATH_TXTUPL.DS.'especes.txt';
      $tmp_src   = $userfile['tmp_name'];
      
      // Write the file $tmp_src in $tmp_dest folder
      if ( JFile::upload($tmp_src, $tmp_dest ) )
      {
         return true;
      }
      else
      {
         return false;   
      }
   }


but nothing happens

I also registered the task in the constructor of my controller:

Code: Select all

   function __construct()
   {
       parent::__construct();

       // Register Extra tasks
      // wir mappen add zu edit... edit ist ja nix anders wie add nur mit gefüllter form etc..
       $this->registerTask( 'add'  ,     'edit' );
      $this->registerTask( 'upload'  ,     'upload' );
   }

Re: What API calls to add upload option?

Posted: Wed Oct 03, 2007 6:16 am
by ianmac
check where you're getting your task from?  And check to make sure it is being retrieved properly.  You are sending it via get, and it might be looking for it via post.

Ian

Re: What API calls to add upload option?

Posted: Wed Oct 03, 2007 7:40 am
by ghostrifle
Uhh... I'm using POST to send the file....  so.... I have to differ between GET and POST while retrieving a task?... that's the thirst thing I read now considering joomla tasks.... where can I get further information about this topic and the GET and POST differences in joomla tasks !?

Thanx, Alex

Re: What API calls to add upload option?

Posted: Wed Oct 03, 2007 9:59 pm
by ghostrifle
Well... I found the error... I nested two forms ... it works now, thanx for your help !