1

Possible Duplicate:
File Upload via AJAX within JQuery
How to easily upload files without form submission (with jQuery + AJAX)

I know for a fact that we can upload files using forms with enctype="multipart/form-data" but what i'm trying to figure out is upload files using Jquery Ajax..

Any tips?? Thanks in advance.

Community
  • 1
  • 1

5 Answers5

0

Try this jQuery plugin http://valums.com/ajax-upload/

Then use this javascript code

var uploader = new qq.FileUploader({
   // pass the element
   element: $(selector)[0],
   // path to server-side upload script
   action: '/server/upload'
});

For more info check the documentation

Cyril Tourist
  • 551
  • 5
  • 13
0

I have used http://blueimp.github.com/jQuery-File-Upload/ in the past and it comes up with great demo(s) and documentation.

Check it out on https://github.com/blueimp/jQuery-File-Upload

frictionlesspulley
  • 11,070
  • 14
  • 66
  • 115
0

depends on your needs.

for single file,http://valums.com/ajax-upload/ is good enought

for multipla files upload + multi file selects you will need other technologies like flash or html5, you can check plupload or Uploadify

plupload supports flash, html5, silvernight html4 upload methods. and also support multi files select (except for html4)

uploadify supports flash and html5 for multi file selects

Yichz
  • 9,250
  • 10
  • 54
  • 92
0

The mentioned plugins are all useful but if you would like to know the logic behind the process its something like this:

  1. Create a php file that handles uploading files and can return errors (echo).
  2. Create an HTML page with your form and everything
  3. Create a jquery function to :
    • Avoid the form from being submitted
    • create an ajax request to your php file you created in the first step
    • show the result from the php file in a div.
Ahoura Ghotbi
  • 2,866
  • 12
  • 36
  • 65
0

You need to use FormData object, but it will work only in newer browsers.

if (window.FormData) {
    $('input[type=file]').change(function() {
        var formdata = new FormData();
        var file = this.files[0];
        formdata.append("files[]", file);
        $.ajax({
            url: "upload.php",
            type: "POST",
            data: formdata,
            processData: false,
            contentType: false
        });
    });
}

Some more info: https://developer.mozilla.org/en/XMLHttpRequest/FormData

You can also see lib with example HTML and some additional features: http://net.tutsplus.com/tutorials/javascript-ajax/uploading-files-with-ajax/

For supporting older browsers, you can make iframe, clone file input element to the iframe and submit form there. In this way, page will not refresh and it will be AJAX-like.

Marius Balčytis
  • 2,601
  • 20
  • 22