How can I send multiple file to the server using below HTML elements:
<input name="file1" type="file" />
<input name="file2" type="file" />
<input name="file3" type="file" />
Knowing that, using <input type="file" multiple />
element data can be sent as:
const formData = new FormData();
const photos = document.querySelector('input[type="file"][multiple]');
formData.append('title', 'My Vegas Vacation');
for (let i = 0; i < photos.files.length; i++) {
formData.append('photos', photos.files[i]);
}
fetch('https://example.com/posts', {
method: 'POST',
body: formData,
})
.then(response => response.json())
.then(result => {
console.log('Success:', result);
})
.catch(error => {
console.error('Error:', error);
});