Skip to content Skip to sidebar Skip to footer

Saving Files In A Chrome App

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 limitati

Solution 1:

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) }
    );
    

Post a Comment for "Saving Files In A Chrome App"