I'm new to Javascript and jQuery. While uploading N number of files in the form, only the last file is uploaded N times,
I'm want to upload each file one at a time (Asynchronously) through AJAX request.
Below is my implementation:
$("#file-input").on("input", function() {
var formdata = new FormData(document.getElementById("file-catcher"));
$.each($("#file-input")[0].files, function (key, file){
formdata.append(key, file);
// This is to Inspecting each file
for(var pair of formdata.entries()) {
console.log(pair[0]+ ': '+ pair[1]);
}
// Sending AJAX
$.ajax({
type: "POST",
data: formdata,
url: "/url/to/upload",
datatype: 'JSON',
contentType: false,
cache: false,
processData: false,
success:function(data){
console.log(data); // Inspecting Response
},
error: function (error) {
console.log(error); // Inspecting Error if occured
}
});
});
});
<form method="POST" id="file-catcher" action="/url/to/upload" enctype="multipart/form-data">
<input type="text" name="fileCode" value="10">
<input type="file" id="file-input" name="file-input" multiple>
</form>