Skip to content

Commit f824ae3

Browse files
abelperezokmkArtakMSFT
authored andcommitted
Added input file support for ajax form post (#1)
* Added input file support for ajax form post * Applied the change to source file and updated dist output
1 parent 7e7b977 commit f824ae3

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

dist/jquery.unobtrusive-ajax.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,30 @@
116116
options.data.push({ name: "X-HTTP-Method-Override", value: method });
117117
}
118118

119+
// change here:
120+
// Check for a Form POST with enctype=multipart/form-data
121+
// add the input file that were not previously included in the serializeArray()
122+
// set processData and contentType to false
123+
var $element = $(element);
124+
if ($element.is("form") && $element.attr("enctype") == "multipart/form-data") {
125+
var formdata = new FormData();
126+
$.each(options.data, function (i, v) {
127+
formdata.append(v.name, v.value);
128+
});
129+
$("input[type=file]", $element).each(function () {
130+
var file = this;
131+
$.each(file.files, function (n, v) {
132+
formdata.append(file.name, v);
133+
});
134+
});
135+
$.extend(options, {
136+
processData: false,
137+
contentType: false,
138+
data: formdata
139+
});
140+
}
141+
// end change
142+
119143
$.ajax(options);
120144
}
121145

dist/jquery.unobtrusive-ajax.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jquery.unobtrusive-ajax.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,30 @@
116116
options.data.push({ name: "X-HTTP-Method-Override", value: method });
117117
}
118118

119+
// change here:
120+
// Check for a Form POST with enctype=multipart/form-data
121+
// add the input file that were not previously included in the serializeArray()
122+
// set processData and contentType to false
123+
var $element = $(element);
124+
if ($element.is("form") && $element.attr("enctype") == "multipart/form-data") {
125+
var formdata = new FormData();
126+
$.each(options.data, function (i, v) {
127+
formdata.append(v.name, v.value);
128+
});
129+
$("input[type=file]", $element).each(function () {
130+
var file = this;
131+
$.each(file.files, function (n, v) {
132+
formdata.append(file.name, v);
133+
});
134+
});
135+
$.extend(options, {
136+
processData: false,
137+
contentType: false,
138+
data: formdata
139+
});
140+
}
141+
// end change
142+
119143
$.ajax(options);
120144
}
121145

0 commit comments

Comments
 (0)