I modify the jquery.unobtrusive-ajax.js
to work uploading files.
First modification:
$(document).on("submit", "form[data-ext=true]", function (evt) {
var clickInfo = $(this).data(data_click) || [],
clickTarget = $(this).data(data_target),
isCancel = clickTarget && clickTarget.hasClass("cancel");
evt.preventDefault();
if (!isCancel && !validate(this)) {
return;
}
var formData;
if (this.enctype && this.enctype === "multipart/form-data") {
formData = new FormData(this);
} else {
formData = clickInfo.concat($(this).serializeArray());
}
asyncRequest(this, {
url: this.action,
type: this.method || "GET",
data: formData
});
});
Second modification is in asyncRequest
:
....
method = options.type.toUpperCase();
if (options.data instanceof FormData) {
options.processData = false;
options.contentType = false;
options.data.append("X-Requested-With", "XMLHttpRequest");
if (!isMethodProxySafe(method)) {
options.type = "POST";
options.data.append("X-HTTP-Method-Override", method);
}
} else {
options.data.push({ name: "X-Requested-With", value: "XMLHttpRequest" });
if (!isMethodProxySafe(method)) {
options.type = "POST";
options.data.push({ name: "X-HTTP-Method-Override", value: method });
}
}
...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…