Chrome FileSystem API中的QUOTA_EXCEEDED_ERR

时间:2012-11-20 13:19:15

标签: javascript html5 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();
});​

1 个答案:

答案 0 :(得分:2)

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