最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - unlimited file storage in chrome app - Stack Overflow

programmeradmin2浏览0评论

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 storage is here: , which says that it is only available to Chrome apps.

All of the examples I have seen of the Chrome fileSystem API suggest that in order to create a new file and write to it, the user needs to obtain an entry object from the chrome.fileSystem.chooseEntry function, which means the user needs to go through a dialog box.

The regular fileSystem API looks like it can save a file to the user's extension sandbox without making the user go through a dialog box, but I'm wondering, does it have the "unlimitedStorage" permission that is only given to apps? It wouldn't shock me if only the Chrome API has that permission.

If I use the regular fileSystem API, I need to request the filesystem using window.webkitRequestFileSystem (I'm a little surprised window.requestFileSystem isn't working for me in Chrome), but the method takes type ("persistent" or "temporary"), and size (the bytes the app will require for storage) arguments.

You obviously don't do that in the Chrome API. However, the normal fileSystem API lets you request more storage when you need it using the Quota Management API, which is also documented on the page I linked above

And on , it says:

The "permissions" field lets you specify HTML5 permissions that the app requires. By specifying "unlimitedStorage" and "notifications", this app is able to use those HTML5 features without having to repeatedly ask the user for permission. During app installation, the user is told which permissions the app needs; installing the app implicitly grants those permissions for all pages whose URLs match those in the "apps" field of the manifest.

So I'm hoping that the "unlimitedStorage" permission simply means that I can just call window.webkitRequestFileSystem(window.PERSISTENT, anySize, successCallback), and instead of asking the user to raise the quota when it needs to, Chrome automatically does it.

Question 1: Does anyone know if that's the way it works?

However, if only the the chrome API has that "unlimitedStorage" permission, I could try creating a file entry with the regular API, and passing that entry into the Chrome API functions, therefore skipping the need for a dialog box, and gaining access to the "unlimitedStorage" permission.

Question 2: Is it possible for me to create the entry in one API and pass it to the other? I'm afraid the different API's might have different sandboxes.

The documentation on the fileSystem API's isn't great, so I figured I'd ask to see if anybody knew from previous experience. The fewer rabbit holes the better.

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 storage is here: https://developers.google./chrome/whitepapers/storage, which says that it is only available to Chrome apps.

All of the examples I have seen of the Chrome fileSystem API suggest that in order to create a new file and write to it, the user needs to obtain an entry object from the chrome.fileSystem.chooseEntry function, which means the user needs to go through a dialog box.

The regular fileSystem API looks like it can save a file to the user's extension sandbox without making the user go through a dialog box, but I'm wondering, does it have the "unlimitedStorage" permission that is only given to apps? It wouldn't shock me if only the Chrome API has that permission.

If I use the regular fileSystem API, I need to request the filesystem using window.webkitRequestFileSystem (I'm a little surprised window.requestFileSystem isn't working for me in Chrome), but the method takes type ("persistent" or "temporary"), and size (the bytes the app will require for storage) arguments.

You obviously don't do that in the Chrome API. However, the normal fileSystem API lets you request more storage when you need it using the Quota Management API, which is also documented on the page I linked above

And on https://developers.google./chrome/apps/docs/developers_guide#manifest, it says:

The "permissions" field lets you specify HTML5 permissions that the app requires. By specifying "unlimitedStorage" and "notifications", this app is able to use those HTML5 features without having to repeatedly ask the user for permission. During app installation, the user is told which permissions the app needs; installing the app implicitly grants those permissions for all pages whose URLs match those in the "apps" field of the manifest.

So I'm hoping that the "unlimitedStorage" permission simply means that I can just call window.webkitRequestFileSystem(window.PERSISTENT, anySize, successCallback), and instead of asking the user to raise the quota when it needs to, Chrome automatically does it.

Question 1: Does anyone know if that's the way it works?

However, if only the the chrome API has that "unlimitedStorage" permission, I could try creating a file entry with the regular API, and passing that entry into the Chrome API functions, therefore skipping the need for a dialog box, and gaining access to the "unlimitedStorage" permission.

Question 2: Is it possible for me to create the entry in one API and pass it to the other? I'm afraid the different API's might have different sandboxes.

The documentation on the fileSystem API's isn't great, so I figured I'd ask to see if anybody knew from previous experience. The fewer rabbit holes the better.

Share Improve this question edited Oct 8, 2014 at 14:43 Xan 77.7k18 gold badges197 silver badges217 bronze badges asked Oct 4, 2014 at 19:18 markainmarkain 3795 silver badges15 bronze badges 4
  • The scope of your question is not clear from it. Do you want to achieve "I want to save an unlimited number of files to the users hard drive" from: a webpage; a webpage in a hosted app; an extension; a packaged app? – Xan Commented Oct 5, 2014 at 7:59
  • My extension downloaded a blob from a website, and sent it to my packaged app, where I want to put it on my user's hard drive – markain Commented Oct 5, 2014 at 15:06
  • Please edit your question: remove "my solution" section and post it as a separate answer. If you want to give credit to @Marc, just keep his answer accepted. – Xan Commented Oct 8, 2014 at 12:01
  • The Chrome docs you quote specifically refer to "Installable Web Apps", also known as "hosted apps" in Chrome documentation. They are very different from normal Chrome Apps. – Xan Commented Oct 8, 2014 at 14:44
Add a ment  | 

2 Answers 2

Reset to default 2

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 puter'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.

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 = new Blob([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

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

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

<button id="input"> Chose a place to save your files</button> 
发布评论

评论列表(0)

  1. 暂无评论