I have an array of files that you receive files of an input type = file. I can not use the input because the user must have the ability to cancel a file. I need to pass this file array to my webservice in Java using ajax. How I spend my array using the method POST without using the form?
My big problem is the token and the header, the two need be dynamic and I deal in form, but in this case, I do not a form, I want to spend a var filearray declared in JavaScript. I cannot use a jQuery library file upload, because I need to show the files in other div after an user cancel a file and I have no idea how I show the files in other div because the library clean the input after the user chooses the files. I do not know where the library puts the files for me to handle them. "var filearray = []" is a global variable.
My HTML:
<input type="file" id="myFiles" multiple size="50" onchange="myFilesFunction()">
My JS:
function myFilesFunction(){
var x = document.getElementById("myFiles");
var contfilearray = 0;
for (var i = 0; i < x.files.length; i++) {
filearray[contfilearray] = x.files[i];
contfilearray ++;
}}
My Ajax:
function salvaanexoszip() {
var numeroticket = 12345;
var data = new FormData();
jQuery.each(jQuery(filearray)[0], function(i, file) {
data.append('file-'+i, file);
});
$.ajax({
url: 'URL',
type: 'POST',
cache: false,
contentType: false,
processData: false,
data: {
ticket: numeroticket,
files: data,
},
success: function(data) {
alert("OK");
},
error: function (xhr, ajaxOptions, thrownError) {
alert("NOT OK");
}
});}