Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a Chrome Extension download a file

I am creating an extension that will download a mp3 file off a website. I am trying to do this by creating a new tab with the link to the mp3 file, but chrome keeps opening it inside the player instead of downloading it. Is there any way I can create a pop-up to ask the user to "save-as" the file?

like image 731
Franz Payer Avatar asked Jan 30 '11 20:01

Franz Payer


People also ask

Can you download Chrome extensions as a file?

In the Chrome Web Store, go to the page for the extension you want. In the address bar, copy the extension's URL. Open the Chrome Extension Downloader in a browser by visiting https://chrome-extension-downloader.com/. Paste the URL into the text box and select Download extension.

How can I force Chrome to only download one file at a time?

In the Privacy and security section, select Content settings. Select Automatic downloads, and then turn on Do not allow any site to download multiple files automatically. Chrome will now ask your permission before downloading multiple files.

How do I make Chrome download a folder?

Once you have downloaded a file, you can quickly and easily move it to another location by dragging and dropping it from the “Downloads” list in Chrome to a folder in File Explorer or any other file browser you are using.


1 Answers

Fast-forward 3 years, and now Google Chrome offers chrome.downloads API (since Chrome 31).

After declaring "downloads" permission in the manifest, one can initiate a download with this call:

chrome.downloads.download({   url: "http://your.url/to/download",   filename: "suggested/filename/with/relative.path" // Optional }); 

If you want to generate the file content in the script, you can use Blob and URL APIs, e.g.:

var blob = new Blob(["array of", " parts of ", "text file"], {type: "text/plain"}); var url = URL.createObjectURL(blob); chrome.downloads.download({   url: url // The object URL can be used as download URL   //... }); 

For more options (i.e. Save As dialog, overwriting existing files, etc.), see the documentation.

like image 189
Xan Avatar answered Oct 13 '22 22:10

Xan