Categories
JavaScript Answers

How to create a new file with JavaScript?

Spread the love

To create a new file with JavaScript, we use the File constructor.

For instance, we write

const f = new File([""], "filename");

to call File with an array with the file contents and a string with the filename respectively.

We can pass in a third argument with some options.

For example, we write

const f = new File([""], "filename.txt", {
  type: "text/plain",
  lastModified: date,
});

to call File with an object with the MIME type and the lastModified date of the file as the 3rd argument.

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 *