1

我的 google chrome 应用程序和文件存储有问题。

该应用程序在线运行并收集文件以离线存储,以便以后能够在离线时正常运行。它在大多数情况下都会这样做,但有时但很少在计算机重新启动或重新启动浏览器后,它似乎丢失了文件系统中的文件......

我想我的问题是,我如何确保持久存储保持持久性?

编辑:

代码

请求文件系统

window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(
window.PERSISTENT, 200*1024*1024,
    function(filesystem) {
        directory.fs = filesystem;
        //Start Application
    },
    filesystemerrorHandler
);

将文件从远程保存到本地文件系统

var xhr = new XMLHttpRequest();
xhr.open('GET', fileurl, true);
xhr.responseType = 'blob';
xhr.send();
xhr.onload = function(e) {
    if (this.status == 200) {
        var blob = new Blob([this.response], {type: blobtype});
        directory.fs.root.getFile(name, {create: true}, function(fileEntry) {
            fileEntry.createWriter(function(writer) {
                writer.onwrite = function(e) {};
                writer.onerror = function(e) { console.log("error"); console.log(e); };
                var blob = new Blob([xhr.response], {type: blobtype});
                writer.write(blob);
                var url = fileEntry.toURL();
                if ( typeof(callback) == 'function' ) {
                    //Save url to indexeddb for recall later
                    //Returns format of: filesystem:chrome-extension://nlipipdnicabdffnohdhhliiajoonmgm/persistent/xxxxxxxxxxxx.png
                    callback(url);    
                }
            }, filewriteerrorHandler2);
        }, filewriteerrorHandler);
    }
    else {
        if ( typeof(callback) == 'function' ) callback(false);
    }
};

回忆下载的文件示例

<img src="filesystem:chrome-extension://nlipipdnicabdffnohdhhliiajoonmgm/persistent/xxxxxxxxxxxx.png">

现在在大多数情况下,这将起作用。但是,有时,如果计算机已重新启动或浏览器已重新启动。如果我再次使用该应用程序,图像将不会显示,这给我的印象是该应用程序的文件系统已被清除。

我应该注意哪些步骤或哪些区域来防止这种情况发生?

谢谢。

4

1 回答 1

1

增加分配给应用程序的字节数起作用。我存储的比分配的多。

window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(
    window.PERSISTENT, 200*1024*1024,    <====
    function(filesystem) {
        directory.fs = filesystem;
        //Start Application
    },
    filesystemerrorHandler
);
于 2013-11-22T17:49:37.340 回答