Welcome Guest, Not a member yet? Register   Sign In
PHP Upload with an Ajax call
#1

[eluser]Unknown[/eluser]
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.

Code:
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:
Code:
$("#upload-button").click(function(e){

e.preventDefault();

//make ajax call

});
#2

[eluser]cloude[/eluser]
that would depend on how you make the ajax call i guess.

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

might work
never used it myself
#3

[eluser]Unknown[/eluser]
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.
#4

[eluser]Rok Biderman[/eluser]
You actually shouldn't have any problems calling a CI method inside a post request. Anything like this should work:

Code:
$.post('<?php echo site_url('contoller/method'); ?>', {
      variable_1: somevalue,
      variable2: anothervalue
      },
      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.
#5

[eluser]Aken[/eluser]
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/53923...query-ajax
#6

[eluser]Ajaxboy[/eluser]
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/plu...oadify.php

http://cjax.sourceforge.net/examples/plu..._files.php




Theme © iAndrew 2016 - Forum software by © MyBB