HTML5 Filesystem API:下载沙盒文件?

Pol*_*lly 6 html5 html5-filesystem

我在indexeddb文件中有一些数据,我想让我的用户下载该数据以导入到其他应用程序中.我可以使用Blob轻松完成此操作:

 var blob = new Blob(['herp!'], {type: 'application/octet-stream'});
 var iframe = document.createElement("iframe");
 iframe.id = "myiframe";
 iframe.style.display = "none";
 iframe.src = window.webkitURL.createObjectURL(blob); // needs a revokeObjectURL
 $('#dlplaceholder').append(iframe);
Run Code Online (Sandbox Code Playgroud)

Blob的正确类型参数是关键:如果我将其设置为text/plain,则不会下载该文件.如果我有iframe的高度和宽度以及块的显示类型,我可以在iframe中看到数据.

但是,我想要从我的数据库中使用多个值构建一个文件,并且每个值都可以很大,并且我可以有很多条目.我可以在Blob构造函数中连接这些值,但我担心在内存中有这么多数据我会爆炸.

所以我决定在沙盒系统中创建一个文件,并将每个值附加到它,并使用该文件而不是blob作为createObjectURL的参数.但是当我这样做时,它不会下载; 它的行为就像我使用一种text/plain类型的Blob代码一样.这是代码:

var fname = 'mybadtest'; //UPDATE here's the problem: needs .bin file extension
var init_fs = function ( fs ) {
  // first, write to the sandboxed file
  fs.root.getFile(fname, {create: true, exclusive: true}, function(fileEntry) {
    fileEntry.createWriter(function(fileWriter) {
      fileWriter.onwriteend = function(e) {
        // write to file completed. now use the file for download
        fs.root.getFile(fname, {}, function(fileEntry) {
          fileEntry.file(function(file) {
            var iframe = document.createElement("iframe");
            iframe.id = "myiframe";
            iframe.style.display = "none";
            iframe.src = window.webkitURL.createObjectURL(file) // needs revokeObjURL                                        
            $('#dlplaceholder').append(iframe);
          }, errorHandler);
        }, errorHandler);
     };
     var blob = new Blob(['derp!'], {type: 'application/octet-stream'});
     fileWriter.write(blob);
   }, errorHandler);
  }, errorHandler);
};

window.webkitStorageInfo.requestQuota(PERSISTENT, mysz, function(grantedBytes) {
  window.webkitRequestFileSystem(PERSISTENT, grantedBytes, init_fs, errorHandler);
}, err_function );
Run Code Online (Sandbox Code Playgroud)

如果我给iframe一个大小并将显示设置为阻止,我可以看到我写入文件的数据.所以我知道我的文件编写正确.

关于如何为文件提供正确类型的任何想法,以便我可以下载它?我已经阅读了MDN文档,HTML5 Rocks tuts和Eric Bidelman的"使用文件系统API"一书,但我没有看到任何解决这种情况的内容.我使用的是Chrome版本27.0.1453.110.

更新:问题解决了; 见下面的评论.

更新更新:请参阅Rob W关于下面替代方法的有用评论.