Skip to content

Commit 47f7cf0

Browse files
author
Hong
committed
deep serialize any extra data params passed in when submitting multipart
1 parent 5904d38 commit 47f7cf0

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

jquery.form.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,19 @@ $.fn.ajaxSubmit = function(options) {
210210
this.trigger('form-submit-notify', [this, options]);
211211
return this;
212212

213+
// This is a utility function to fix deep serialization issues when doing file uploads
214+
// eg params show up as "[object Object]" rather than what it should be
215+
function formDeepSerialize(extraData){
216+
var serialized = $.param(extraData).split('&');
217+
var result = {};
218+
var n;
219+
for(var i=0, l=serialized.length; i < l; i++){
220+
n = serialized[i].split('=')
221+
result[decodeURIComponent(n[0])] = decodeURIComponent(n[1])
222+
}
223+
return result;
224+
}
225+
213226
// XMLHttpRequest Level 2 file uploads (big hat tip to francois2metz)
214227
function fileUploadXhr(a) {
215228
var formdata = new FormData();
@@ -219,9 +232,10 @@ $.fn.ajaxSubmit = function(options) {
219232
}
220233

221234
if (options.extraData) {
222-
for (var p in options.extraData)
223-
if (options.extraData.hasOwnProperty(p))
224-
formdata.append(p, options.extraData[p]);
235+
var serializedData = formDeepSerialize(options.extraData);
236+
for (var p in serializedData)
237+
if (serializedData.hasOwnProperty(p))
238+
formdata.append(p, serializedData[p]);
225239
}
226240

227241
options.data = null;

0 commit comments

Comments
 (0)