Friday, 21 July 2017

javascript - Send input files data with ajax





html markup:







I am uploading multiples files with php. I want to make an array of upload files and send to server with ajax. how to make an array of the multiple selected files?



JavaScript:



jQuery.ajax({
url: 'insertfiles.php',
type: "POST",
data: {
file: // array of selected files.
},

success: function(data){
},
error: function(data){
alert( 'Sorry.' );
}
});

Answer



Modern browsers that support the HTML5 file stuff have in the element a "files" property.
That will give you a file list reference, which has a length property.




As the property is already an array so you just need to access it or iterate through it.



JS



var input = document.getElementById('id');
console.log(input.files);

for (var i = 0; i < input.files.length; i++) {
console.log(input.files[i]);

}

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...