I have a question regarding uploading multiple files to the server using Servlet and Ajax call. There are similar question to my problem on the internet, but I couldn't solve my issue. So my apologies in advance if the question is simple or very obvious. I have two input file inside a form HTML tag. User can browse for the file using file system, and then hit a button to upload them. Here is the HTML code
<form id="uploadFile">
<input id="file1" class="files" type="file" name="file"></input>
<input id="file2" class="files" type="file" ></input>
<input id="processData" class="btn btn-primary" type="submit" name="submit" value="Process Data Sources" />
</form>
and here is my js code
//form Submit
$("form").submit(function(evt){
evt.preventDefault();
var formData = new FormData($(this)[0]);// I guess this value has to change to provide me with information of two files, but I am not sure how this can be done.
$.ajax({
url: 'UploadServlet',
type: 'POST',
data: formData,
async: true,
cache: false,
contentType: false,
enctype: 'multipart/form-data',
processData: false,
success: function (response) {
alert(response);
console.log(response);
}
});
return false;
});
The ajax code uploads one file at the moment, but I want to upload two files that is chosen using 2 input files. I am not sure how I can do it.
My guess from what I read here and there is that I need to modify formData variable in the submit function. I really appreciate if some one can give me hint on this