Belaviyo
Belaviyo

Reputation: 187

Chrome packaged app; copy a fileEntry to user's download directory

So in my app, there is a local storage (fileEntry) created by my app. I would like to add a feature to let user have access to the file. Since the app itself does not have access to the "Downloads" directory, currently I am using the following code:

let link = document.createElement('a');
link.download = obj.name;
link.href = URL.createObjectURL(file);
link.dispatchEvent(new MouseEvent('click'));

where file is grabbed from fileEntry.file(function (file){}). The code works fine, but the problem is my file is huge (500MB) and when I create createObjectURL Chrome uses twice memory as the content is copied in the blob. The only thing I want is to "move" the file from the internal storage to user's "Downloads" directory. I know in extensions one can use the chrome.download API but this API is not available in packaged apps.

Upvotes: 0

Views: 333

Answers (1)

Daniel Herr
Daniel Herr

Reputation: 20438

Use the chrome.filesystem and chrome.storage apis instead. First, add the permissions to manifest.json:

permissions: [ "storage", { "fileSystem": [ "write", "retainEntries", "directory" ] } ]

Then get access to a folder on the actual filesystem and save it for later.

chrome.fileSystem.chooseEntry({ type: "openDirectory" }, function(folder) {
 chrome.storage.local.set({ folder: chrome.fileSystem.retainEntry(folder) })
})

You can then restore it and use it like a normal directory entry.

chrome.storage.local.get("folder", function(storage) {
 chrome.fileSystem.restoreEntry(storage.folder, function(folder) {
  entry.moveTo(folder)
}) })

Upvotes: 1

Related Questions