I have a multiple files upload form:
<input type="file" name="files" multiple />
I am posting these files with ajax. I would like to upload the selected files one by one (to create individual progress bars, and out of curiousity).
I can get the list of files or individual files by
FL = form.find('[type="file"]')[0].files
F = form.find('[type="file"]')[0].files[0]
yieling
FileList { 0=File, 1=File, length=2 }
File { size=177676, type="image/jpeg", name="img.jpg", more...}
But FileList is immutable and I can't figure out how to submit the single file.
I think this is possible as I saw http://blueimp.github.com/jQuery-File-Upload/. I don't want to use this plugin however as it's as much about learning as the result (and it would need too much custimizing anyway). I also don't want to use Flash.
HTML
Javascript
PHP
This looks like a very good tutorial, just what you're looking for.
That noted, uploading via vanilla ajax isn't supported in all browsers, and I would still recommend using an
iframe
. (I.E dynamically create an iframe and POST it using javascript)I have always been using this script and I find it very concise. If you want to learn how to upload via creating an iframe, you should dig through it's source.
Hope it helps :)
Extra edit
To create progress bars with the iframe method, you will need to do some work server-side. If you're using php, you can use:
If use nginx you can also choose to compile with their Upload progress module
All of these work the same way - every upload has a UID, you will request for the 'progress' associated with that ID at consistent intervals via ajax. This will return the upload progress as determined by the server.
I was facing same problem and came with this solution. I simply retrieve form with multipart, and in recursive call ( file after file ) start ajax request, which only calls next when is done.
For this to be a synchronous operation, you need to start the new transfer when the last one is done. Gmail, for example, send everything at once, concurrently. The event for the progress on AJAX file upload is
progress
oronprogress
on the rawXmlHttpRequest
instance.So, after each
$.ajax()
, on the server side (which I don't know what you'll be using), send a JSON response to execute the AJAX on the next input. One option would to bind the AJAX element to each element, to make things easier, so you could just do, in thesuccess
the$(this).sibling('input').execute_ajax()
.Something like this:
The above code would be for multiple
<input type="file">
but not for<input type="file" multiple>
, in that case, it should be: