Admittedly, there are similar questions lying around on Stack Overflow, but it seems none quite meet my requirements.
Here is what I’m looking to do:
- Upload an entire form of data, one piece of which is a single file
- Work with Codeigniter’s file upload library
Up until here, all is well. The data gets in my database as I need it. But I’d also like to submit my form via an AJAX post:
- Using the native HTML5 File API, not flash or an iframe solution
- Preferably interfacing with the low-level
.ajax()jQuery method
I think I could imagine how to do this by auto-uploading the file when the field’s value changes using pure javascript, but I’d rather do it all in one fell swoop on for submit in jQuery. I’m thinking it’s not possible to do via query strings as I need to pass the entire file object, but I’m a little lost on what to do at this point.
Can this be achieved?
It’s not too hard. Firstly, take a look at FileReader Interface.
So, when the form is submitted, catch the submission process and
Then, on the server side (i.e. myscript.php):
Or something like it. I may be mistaken (and if I am, please, correct me), but this should store the file as something like
1287916771myPicture.jpgin/uploads/on your server, and respond with a JSON variable (to acontinueSubmission()function) containing the fileName on the server.Check out
fwrite()andjQuery.post().On the above page it details how to use
readAsBinaryString(),readAsDataUrl(), andreadAsArrayBuffer()for your other needs (e.g. images, videos, etc).