Skip to content

Instantly share code, notes, and snippets.

@llagerlof
Last active August 1, 2018 11:57
Show Gist options
  • Save llagerlof/c51ab4c94c04132f36d09f6c46dadc78 to your computer and use it in GitHub Desktop.
Save llagerlof/c51ab4c94c04132f36d09f6c46dadc78 to your computer and use it in GitHub Desktop.
How to POST form data with multiple files upload using jQuery
<script type="text/javascript">
// This example expect a json return from receiver_script.php
// Remove unnecessary logs and alerts as needed.
var form_data = new FormData();
form_data.append("first_name", $("#first_name").val());
form_data.append("age", $("#age").val());
var file_data = $("#attachment").prop("files");
$.each(file_data, function (i, file) {
form_data.append("attachment["+i+"]", file);
});
$.ajax({
type: "POST",
contentType: false,
processData: false,
url: "receiver_script.php",
data: form_data,
dataType: "json",
cache: false,
success: function(result) {
console.log("result:", result);
$.each(result, function(key, value) {
console.log(key);
console.log(value);
});
},
error: function(error) {
alert("error: " + error.responseText);
console.log("error: " + error.responseText);
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment