-
-
Save armababy/240075394b9aa516d92408185783405f to your computer and use it in GitHub Desktop.
Fileuploads via jquery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Somewhere | |
(function ($) { | |
$.fn.serializefiles = function () { | |
var obj = $(this); | |
/* ADD FILE TO PARAM AJAX */ | |
var formData = new FormData(); | |
$.each($(obj).find("input[type='file']"), function (i, tag) { | |
$.each($(tag)[0].files, function (i, file) { | |
formData.append(tag.name, file); | |
}); | |
}); | |
var params = $(obj).serializeArray(); | |
$.each(params, function (i, val) { | |
formData.append(val.name, val.value); | |
}); | |
formData.append('ajax', true); | |
return formData; | |
}; | |
})(jQuery); | |
// In ajax call | |
// Use .serializefiles() on form data usually data: $(this).serializefiles(); | |
// Add option processData: false, to ajax call |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment