Categories
JavaScript Answers

How to save a file on disk in a Chrome extension with JavaScript?

Spread the love

Sometimes, we want to save a file on disk in a Chrome extension with JavaScript.

In this article, we’ll look at how to save a file on disk in a Chrome extension with JavaScript.

How to save a file on disk in a Chrome extension with JavaScript?

To save a file on disk in a Chrome extension with JavaScript, we can use the chrome.downloads.download method.

For instance, we write

const url = "https://picsum.photos/200/300";

chrome.downloads.download({ url }, (downloadId) => {
  console.log(downloadId);
});

to call chrome.downloads.download with { url } and a callback that runs when the download starts.

We download the file at the url.

Conclusion

To save a file on disk in a Chrome extension with JavaScript, we can use the chrome.downloads.download method.

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 *