Is it possible to do this? In more detail:
- I have a file object which I have retrieved from an input element
- I want to upload this to the server, together with a couple of key/value pairs with related data (file name, and so on)
- I want to do this with Ajax, because everthing else in the system uses Ajax, and I have standardised success/fail handling based on the server response
- I want to do this with URL encoding, because the file is small, and
the server doesn't implement
multipart/form-data
I've tried several different things, but none of them is quite right. This code sends the file data 'as-is' (ie. effectively raw binary, and not as a URL-encoded key-value pair):
var files = document.getElementById('idUploadBrowseInput').files;
var fdata = files[0];
var jqxhr = $.ajax({
type : "POST",
url : "/cgi-bin/upload_cgi",
timeout : 20000,
processData : false,
data : fdata
}).always(...etc)
Raw binary would be Ok, but I also need the other key-value pairs, and it doesn't seem to be possible to send these at the same time.
If I change the data : fdata
line to data : {fdata : fdata}
then the POST data just becomes [object Object]
(on FF). If I remove the processData:false
then I get an exception in jQuery.
The FileReader API has some readAs
methods which I guess I should be using - presumably, if I read the file contents into a var
, then I can just send the var as part of the data object and it should all just work. However, this is long-winded, and I just want to check that there isn't a more direct way of doing this first.
Note that this post doesn't answer the question.