How could I do with Javascript to generate a input:file
multiple but taking the files from different input files, let me explain:
<inpu type="file" name="file1">
<inpu type="file" name="file2">
<inpu type="file" name="file3">
To send them with Javascript using ajax you should do something like this:
fd = new FormData();
xhr = new XMLHttpRequest();
fd.append('file1', file1, file1.name);
fd.append('file2', file2, file2.name);
fd.append('file3', file3, file3.name);
xhr.open('post', url, true);
xhr.send(fd);
What I need (because my application creates different fields input:file
dynamically and I don't have to change that functionality) is to use code similar to this:
fd.append('archivos', files, "archivos");
Where "files" is an array of files.
Why must it be so?
Because in Node I am using Multer and to upload multiple files I must use a middleware like the following:
api.post('/equipos/uploadfiles', upload.array('archivos', 12), equipoController.uploadFiles);
Data: I am using Angular and I already have the file upload ready, but one by one.
The solution to my problem was really simple and I share it with you:
I have added all the files to an array and then iterated them as follows:
I hope it helps someone.