Categories
JavaScript Answers

How to upload multiple files using form data with JavaScript?

Spread the love

Sometimes, we want to upload multiple files using form data with JavaScript.

In this article, we’ll look at how to upload multiple files using form data with JavaScript.

How to upload multiple files using form data with JavaScript?

To upload multiple files using form data with JavaScript, we can call the append with the same key for multiple files.

For instance, we write

const { files } = document.getElementById("fileupload");
const formData = new FormData();
for (const image of files) {
  formData.append("files", image);
}

to select the file input with getElementById.

Then we get the selected files with files.

Next, we create the FormData object.

Then we loop through the files with a for-of loop and call formData.append with the 'files' key and add each image to it.

Conclusion

To upload multiple files using form data with JavaScript, we can call the append with the same key for multiple files.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *