EllisLab text mark
Advanced Search
     
PHP Upload with an Ajax call
Posted: 07 October 2012 07:55 PM   [ Ignore ]
Joined: 2012-10-07
2 posts

I’m trying to upload files using an Ajax call; I’ve been searching the webs for the last few days and found some plugins; however, is it possible to use my current function:

it works as a standalone function to be called; but I can’t get it to be called via JS.
Thanks.

function changeBookPicture($table$bookId)
 
{
  
  $config[
'upload_path'"Marketplace_Pictures/$table/";
  
$config['allowed_types''jpeg|png|jpg';
  
$config['max_size''1000000';
  
$config['overwrite'true;
  
$config['file_name'$bookId;

  
$this->load->library('upload'$config);
  
$this->upload->do_upload();
  
  if ( ! 
$this->upload->do_upload())
  
{
   
//upload fail
   
$data['error'= array('error' => $this->upload->display_errors());
  
}
  
else
  
{
   
//upload succeeds
   
$data['error'= array('upload_data' => $this->upload->data());

   
//get uploaded file information
   
$img_data $this->upload->data();
   
//get File extension
   
$ext $img_ext $img_data['file_ext'];
   
//add extension to db
   
$this->marketplace_model->addUserImage($table$bookId$ext);
  
  
}
 } 

This is how I want to call the upload function:

$("#upload-button").click(function(e){

e
.preventDefault();

//make ajax call

}); 
 
Posted: 07 October 2012 09:11 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2012-10-06
4 posts

that would depend on how you make the ajax call i guess.

http://jquery.malsup.com/form/

might work
never used it myself

 
Posted: 08 October 2012 01:55 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2012-10-07
2 posts

I doubt that would work.

I can grab that portion of the view with another ajax call to refresh the picture; however, i want to be able to upload the image with an ajax that calls the changeBookImage function.

I can manage after the upload has been successful.

 
Posted: 09 October 2012 04:08 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2010-09-24
240 posts

You actually shouldn’t have any problems calling a CI method inside a post request. Anything like this should work:

$.post('<?php echo site_url('contoller/method'); ?>'{
      variable_1
somevalue,
      
variable2anothervalue
      }

      function(
response{
          alert(response)
;
      
}); 


The main problem with what you’re trying to do is that Javascript has no access to local files (security issues), so there is no way you can make it solely as an AJAX post request. It has to be done inside a iframe, like all the other “ajax” upload solutions out there.

I’ve posted an example of Codeigniter/Uploadify integration for another user just a few days ago. Take a look at it and see if you can use it.

 Signature 

Taskhub - Your local Marketplace for tasks and errands

 
Posted: 09 October 2012 06:45 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2007-11-28
2435 posts

The new FormData Javascript class makes uploading via ajax super easy. It just isn’t supported by all browsers (specifically Internet Explorer).

http://stackoverflow.com/questions/5392344/sending-multipart-formdata-with-jquery-ajax

 
Posted: 11 October 2012 06:31 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2012-04-18
218 posts

If it helps Cjax has two built in plugins that can upload files through flash + jquery and other without jquery:

http://cjax.sourceforge.net/examples/plugin_uploadify.php

http://cjax.sourceforge.net/examples/plugin_upload_files.php

 Signature 

CjaxFw for CI,  10,000 downloads in last few months, and growing fast

http://cjax.sourceforge.net/

Follow Me on Twitter @AjaxFramework
https://twitter.com/ajaxframework

Checkout my Blog