Skip to content

Instantly share code, notes, and snippets.

@oswaldoacauan
Created November 21, 2013 12:06
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save oswaldoacauan/7580474 to your computer and use it in GitHub Desktop.
Save oswaldoacauan/7580474 to your computer and use it in GitHub Desktop.
jQuery - Serialize Form with File inputs
(function($) {
$.fn.serializeFiles = function() {
var form = $(this),
formData = new FormData()
formParams = form.serializeArray();
$.each(form.find('input[type="file"]'), function(i, tag) {
$.each($(tag)[0].files, function(i, file) {
formData.append(tag.name, file);
});
});
$.each(formParams, function(i, val) {
formData.append(val.name, val.value);
});
return formData;
};
})(jQuery);
@Selman555
Copy link

Line 4 is missing a comma at the end ;)

@pch4115209
Copy link

semi-colon missed in line 4

@pewdee
Copy link

pewdee commented Oct 1, 2018

semi-colon missed in line 4

not a semicolon, but a comma solves.

@gurubobnz
Copy link

Be sure to use a comma on line 4, not a semicolon, otherwise formParams will be declared in the global variable space.

@drumer2142
Copy link

Very helpful !!!

@mmhashemi
Copy link

it's very good idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment