Previously using Prototype I could serialize the input type file, it gave me the file name of the file being uploaded but when I serialized the form in jquery I just got the input type text and others not the file how do I do it?
Relevant jQuery bug: http://bugs.jquery.com/ticket/2656
I added my own serializeAll method instead of serialize:
(function($) {
$.fn.serializeAll = function() {
var rselectTextarea = /^(?:select|textarea)/i;
var rinput = /^(?:color|date|datetime|datetime-local|email|file|hidden|month|number|password|range|search|tel|text|time|url|week)$/i;
var rCRLF = /\r?\n/g;
var arr = this.map(function(){
return this.elements ? jQuery.makeArray( this.elements ) : this;
})
.filter(function(){
return this.name && !this.disabled &&
( this.checked || rselectTextarea.test( this.nodeName ) ||
rinput.test( this.type ) );
})
.map(function( i, elem ){
var val = jQuery( this ).val();
return val == null ?
null :
jQuery.isArray( val ) ?
jQuery.map( val, function( val, i ){
return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
}) :
{ name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
}).get();
return $.param(arr);
}
})(jQuery);
Then call:
$('#form').serializeAll()
After spending a few minutes in Firebug, there's actually several ways to go about this it seems. For instance, I was able to get a list of files from the fileObject itself:
var file = $("#control").attr("files")[0];
var fileName = file.fileName;
var fileSize = file.fileSize;
alert("Uploading: "+fileName+" @ "+fileSize+"bytes");
Clearly I can read the values for serializing. But writing is another issue.
But apparently this isn't as easy as others claim. I took the liberty of downloading the Prototype source and couldn't find out where it's instructions were for using FileList data for the File Upload object.
In fact, I found a paper online that cataloged the issues with serializing the File Upload object itself, claiming that no AJAX Library did it well (note, this was written in 2007). This topic is interesting though, and it appears that you may be able to work up any number of methods to pull the data from the File Upload - the spec itself contains ample information to guide you down that path.
I wish I had an answer as to how you can write, and add files to the File List, but at the moment I'm just as lost as you are (or were at the time of asking this question). With a bit more reading, this may prove to be much easier than I suspect - but at the moment I don't have the time to invest.
Best of luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With