javascript - Chrome 文件系统 API 中的 QUOTA_EXCEEDED_ERR

标签 javascript html html5-filesystem

在 Chrome23/Mac OS 10.8.2 中 this fiddle记录错误,我想知道原因。

令人惊讶的是,如果我在“//BREAKPOINT”行上放置一个断点并简单地继续执行,就不会发生错误。

JS 是否有可能超过 Chrome 中的某些调用速率限制?我想不出更好的解释。

完整代码(我使用的是 lodash,参见其 _.bind and _.bindAll 的文档):

window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.LocalFileSystem = window.LocalFileSystem || {PERSISTENT: window.PERSISTENT};

fs = {
 initFS: function() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT,
                             1024 * 1024, this.gotFS, this.fail);
 },

 fail: function(source, err) {
    err.source = source;
    var msg = '';

    switch (err.code) {
      case FileError.QUOTA_EXCEEDED_ERR:
        msg = 'QUOTA_EXCEEDED_ERR';
        break;
      case FileError.NOT_FOUND_ERR:
        msg = 'NOT_FOUND_ERR';
        break;
      case FileError.SECURITY_ERR:
        msg = 'SECURITY_ERR';
        break;
      case FileError.INVALID_MODIFICATION_ERR:
        msg = 'INVALID_MODIFICATION_ERR';
        break;
      case FileError.INVALID_STATE_ERR:
        msg = 'INVALID_STATE_ERR';
        break;
      default:
        msg = 'Unknown Error';
        break;
    };
    err.msg = msg;
    console.log('err ', JSON.stringify(err));
 },
 failarg: function(msg) {
    return _.bind(this.fail, this, msg);
 },

 gotFS: function(fs) {
    this.fs = this.fs || fs;
    this.readConfig();
    this.listApps(fs.root);  // BREAKPOINT
 },

 listApps: function(fsroot) {
    this.rootReader = this.rootReader || fsroot.createReader();
    this.rootReader.readEntries(this.gotRootEntries, this.fail);
 },

 gotRootEntries: function(entries) {
    _(entries).forEach(function(entry) {
        if (entry.isDirectory && this.controller) {
            // TODO
        }
    });
 },

 readConfig: function() {
    this.fs.root.getFile('config.json', {create:true}, this.gotConfigFileEntry, this.failarg('getFile'));
},
gotConfigFileEntry: function(entry) {
    entry.file(this.gotConfigFile, this.failarg('entry.file'));
},
gotConfigFile: function(file) {
    this.configFile = file;

    var reader = new FileReader();
    reader.onloaded = function(evt) {
        if (evt.target.result) {
            this.config = JSON.parse(evt.target.result);
        }
    };
    reader.readAsText(this.configFile);
},
};
_.bindAll(fs);

$(function() {
   fs.initFS();
});​

最佳答案

以下 Fiddle,基于原始 HTML 5 Rocks 文章 ( http://www.html5rocks.com/en/tutorials/file/filesystem/ ),演示了如何调用配额系统。

http://jsfiddle.net/kim3er/h76qq/1/

window.webkitStorageInfo.requestQuota(PERSISTENT, 1024*1024, function(grantedBytes) {
  //window.requestFileSystem(PERSISTENT, grantedBytes, onInitFs, errorHandler);
  initFS(grantedBytes);
}, function(e) {
  console.log('Error', e);
});

关于javascript - Chrome 文件系统 API 中的 QUOTA_EXCEEDED_ERR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13474089/

相关文章:

javascript - Backbone.js:用一个请求加载多个集合

javascript - 无法从 Iframe 访问父文档

php - 特殊字符转 HTML 字符

javascript - setTimeout -- 立即执行回调?

javascript - 基于选项卡或 Accordion 的页面 - 它是如何在服务器端完成的?

html - Angular 4 在 *ngFor 内调用函数时卡住

html - 在 IOS 上启用画外音时移动菜单未打开

javascript - HTML 5 文件系统访问类型错误

javascript - 在 web worker 和主线程之间传递大量数据

javascript - HTML5 文件系统初始化