How to create a download file in the Google Chrome extension?

I want to create a CSV file as a result of user interaction, and then ask the user to download it. How can i do this?

I don't think this is possible in direct JS, but maybe Chrome gives me some elevated privileges?

+6
google-chrome-extension
source share
3 answers

Now you can use the HTML5 File API to upload the file. It is still under development, but you can use BlobBuilder and redirect your usage to download this file:

var bb = new BlobBuilder(); bb.append(csvContents); var blob = bb.getBlob(); location.href = window.webkitURL.createObjectURL(blob); 

For more information on the File API, HTML5Rocks has an excellent tutorial:

http://www.html5rocks.com/tutorials/file/filesystem/

+5
source share

Chrome does not provide any help with this, but I think you should be able to run the save file dialog using the downloadify js library, which uses the flash component backstage.

+2
source share

I needed to make the same newly created file and ensure that it downloads in the Chrome extension. Here you see a code snippet using ES6 and recommended methods:

 let docContent = ... /* your content */; let doc = URL.createObjectURL( new Blob([docContent], {type: 'application/octet-binary'}) ); chrome.downloads.download({ url: doc, filename: filename, conflictAction: 'overwrite', saveAs: true }); 

You must also declare the correct privilege in manifest.json :

 "permissions" : ["downloads"] 
+1
source share

All Articles