malsup/form — Issue #156: XMLHttpRequest Level 2 file uploads monstruous bugs
Author: softlion
Created: 2011-12-08 18:42:43+00:00
State: open

Hi,
level2 has lots of bugs.
1) if there is more than 1 named submit button, all buttons are included in the data of the ajax call instead of the submitting button
2) all ajax global settings are bypassed, handlers are never called
3) content type is not set correctly, erasing the "boundary" header in the request, breaking automatic bindings of form items in server side frameworks

But i fixed it all :)

Fix for 2 and 3: replace the end of fileUploadXhr by:

var s = $.extend(true, {}, $.ajaxSettings, options, { contentType: false, processData: false, cache: false, type: 'POST' });
s.context = s.context || s;

s.data = null;
var beforeSend = s.beforeSend;
s.beforeSend = function(xhr, o) {
o.data = formdata;
if(xhr.upload) { // unfortunately, jQuery doesn't expose this prop (http://bugs.jquery.com/ticket/10190)
xhr.upload.onprogress = function(event) {
o.progress(event.position, event.total);
};
}
if(beforeSend)
beforeSend.call(o, xhr, options);
};
$.ajax(s);

Fix for 1: in formToArray, replace

if(!n) {
continue;
}

by

if(!n || (el.type == "submit" && el != form.clk)) { //Remove unused submit buttons in case multiple submit buttons are in the form
continue;
}

and in fileUploadIframe completely remove the bloc containing form.clk

// add submitting element to data if we know it
sub = form.clk;
if(sub) {
n = sub.name;
if(n && !sub.disabled) {
s.extraData = s.extraData || {};
s.extraData[n] = sub.value;
if(sub.type == "image") {
s.extraData[n + '.x'] = form.clk_x;
s.extraData[n + '.y'] = form.clk_y;
}
}
}