Skip to content Skip to sidebar Skip to footer

Unlimited File Storage In Chrome App

I want to save an unlimited number of files to the users hard drive, without making the user click through a dialog box. The only documentation I have seen on using unlimited stor

Solution 1:

First of all, to avoid confusion, what here is being called a "packaged app" should be called a "Chrome App".

If the blob is to be used by any other app, you must obtain a FileEntry via chrome.fileSystem.chooseEntry so the data will be written to the computer's file system where the user can access it. (You can obtain a directory that way from the user and save the retained entry, and use that without asking the user to make a choice every time.)

If the blob is only for use by that Chrome App, you can use the file API without calling chrome.fileSystem.chooseEntry.

As for limits, you're correct that unlimitedStorage allows unlimited storage. In my experiments, despite the documentation, I have never been able to get a Chrome App to ask the user to authorize a specific amount. I use that permission, and there seems to be no limit. The Google documentation I've read on this appears to be inaccurate.

If you're writing to the external file system, after you've called chrome.fileSystem.chooseEntry, there are no limits and you don't need unlimitedStorage permission.

Solution 2:

My solution.

main.js

var chooseDirectory = function() {
  chrome.app.window.create("choose_directory.html");
};

chrome.runtime.onInstalled.addListener(function(details) {
  if (details.reason == 'install') {
    chooseDirectory();
  }
};  

chrome.runtime.onMessageExternal.addListener(function(message, sender, sendResponse) {

  switch (message.type) {
  case'saveBlob':
    var blob = newBlob([message.blob])

    chrome.storage.local.get('filesystemKey', function(items) {
      var fileSystemRef = items.fileSystemKey;

      chrome.fileSystem.restoreEntry(fileSystemRef, function(fileSystem) {

        fileSystem.getFile('test.txt', { create: true }, function(fileEntry) {

          fileEntry.createWriter(function(writer) {
            writer.write(blob);
          });   
        });             
      });           
    });  
    break;
  } 
});

after using the chrome filesystem APIs, you use the regular html5 filesystem API to create the file. As explained here you cannot call the chooseDirectory function from the background script, so you need to create an app window that can call it, which I do below.

choose_directory.js

window.addEventListener('load', function() {

  document.getElementById('input').addEventListener('click', function() {

    chrome.fileSystem.chooseEntry({type: 'openDirectory'}, function(entry) {
      var homeDirectory = chrome.fileSystem.retainEntry(entry);

      chrome.storage.local.set({'filesystemKey': homeDirectory}, function() {
        window.close();
      });           
    });     
  });   
}); 

choose_directory.html

<scriptsrc="choose_directory.js"></script>

You need to choose a directory where you will store your files.

<buttonid="input"> Chose a place to save your files</button>

Post a Comment for "Unlimited File Storage In Chrome App"