Skip to content

Instantly share code, notes, and snippets.

@mcunha98
Created June 24, 2020 23:15
Show Gist options
  • Select an option

  • Save mcunha98/cfcc0cea98590c8119441c4dea545780 to your computer and use it in GitHub Desktop.

Select an option

Save mcunha98/cfcc0cea98590c8119441c4dea545780 to your computer and use it in GitHub Desktop.
(function($) {
$.fn.serializeFiles = function() {
var form = $(this);
var formData = new FormData();
var 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);
//exemplo:
//apiPOSTD('banner/salvar', $('#formPrincipal').serializeFiles(), apiCallback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment