Saving files in a Chrome App

2k views Asked by At

Summary

Normally I could download a bunch of files, but Chrome Apps won't show the download shelf when a download occurs. What would be the best way of getting around this limitation of Chrome Apps?

Ideas

  • I could go about this by creating a zip file, but this would require the user to perform an extra step of unzipping the file.
  • I'm able to silently download the files, and so I could display a prompt to the user when the file is downloaded, but this would require the user to manually search for the file in their downloads folder.

What I've Learned

  • Everywhere on the internet tells me to use Chrome's download API, but this only works for Chrome extensions and not Chrome apps.
  • I can't bring up a save as window because 50 save as windows for 50 files is unacceptable
  • I can, however, bring up a prompt using chrome.fileSystem.chooseEntry({'type': "openDirectory"} to ask the user to choose a directory, but I can't find a way of saving to that directory.
  • My question is basically the same as How can a Chrome extension save many files to a user-specified directory? but for a Chrome app instead of an extension.

Project and Example Code

The app I'm building will be the same as this webpage I've built, but with a few modifications to make it work as a web-app.

This is how my website solves the problem

let example_pic = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABcAAAAWCAYAAAArdgcFAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAB3RJTUUH3wQGEjEAJqiN6gAACAlJREFUOBEB/gcB+AFHNgf/DQoBAAoBBwAA+wIA/QABAPcG/AAB+QMAAPb/AAf2AAAA+/8A6AH9AP0NAAAGCAEAAPf8AALj+wAZBwQATWE9AFKFcwAAADoA/wC1AIlslgC5s8YA+vT9AAT3+/8AAPQBAAEC+gACBgIABgkOAAH8/gD1+PwAAvkIAP8BAAD9AfcA/w3/APYIAwD+CQIACAb6APj//gAJBw8ADRwkAAAAEwAAAAgA6OYLAOfV9gD04v0A9AIAAAT29v0A/QcAAAQIAwALAv8A+/v7APwCAgD3+/oAAAH2AAL9AwADC/8AAQcDAAYJAQAEAgYA+v8CAP4I+gDe+PYAubXGAPHu+gAACRUA0MXaAMTN0wDwAAAAA/4AAAEiFQP/FxEBABQLBwAF/AUA9AoDAP3+/AD4+vsA+v4BABD8AQAA/gAA9AEAAPQCAgANDAAABggJAAgA+gD37vg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AAkAA/jvAAf9/QDy8u4ADhEOAPYE9wAFAhEAB/n5AAgCAgAF9f4ACg0DAPX3AQD4BQUAFBIMAAATAQD499gACff+AAj8+AAE/AYAGQcXADGWlu04c4CnAAAAAElFTkSuQmCC"
let a = document.createElement("a");
a.href = example_pic;

document.body.appendChild(a)
a.click();

window.URL.revokeObjectURL(a.href);
a.remove()
1

There are 1 answers

2
Xan On BEST ANSWER

I can, however, bring up a prompt using chrome.fileSystem.chooseEntry({'type': "openDirectory"}) to ask the user to choose a directory, but I can't find a way of saving to that directory.

That's what you need to work on.

Suppose you declare all the sub-permissions for the fileSystem API:

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

Then you can:

  1. Get an entry from the user:

    chrome.fileSystem.chooseEntry({'type': "openDirectory"}, function(dirEntry) {
      // Check for chrome.runtime.lastError, then use dirEntry
    });
    
  2. Retain it, so you can reuse it later without asking the user again:

    dirEntryId = chrome.fileSystem.retainEntry(dirEntry);
    
    // Use chrome.storage to save/retrieve it
    
    chrome.fileSystem.restoreEntry(dirEntryId, function(entry) { /* ... */ });
    
  3. Using the HTML FileSystem API, create files in the directory:

    dirEntry.getFile(
      "test.txt",
      {create: true}, // add "exclusive: true" to prevent overwrite
      function(fileEntry) { /* write here */ },
      function(e) { console.error(e) }
    );