【问题标题】:HTML5 File API with Remote File带有远程文件的 HTML5 文件 API
【发布时间】:2012-02-28 16:26:42
【问题描述】:

我尝试使用 HTML5 文件系统添加一个带有 URL 的远程文件(如 http://example.com/doc.pdf)而不是通过文件输入获得的文件,因为我希望该过程是自动的(我有多个doc),但我没有发现有关该过程的任何信息...

我发现的所有示例都涉及本地文件(添加了文件输入)...

我给你几行代码:

window.requestFileSystem(TEMPORARY, 1024 * 1024 * 10,  function(fs) {
  FS_ = fs;
  addRemoteDocuments();
}, errorCallback);

function addRemoteDocuments(){
var docList = [ "http://cpbdev/html5tests/pdf/tarifs.pdf", "http://cpbdev/html5tests/pdf/tarifs1.pdf", "http://cpbdev/html5tests/pdf/tarifs2.pdf", "http://cpbdev/html5tests/pdf/tarifs3.pdf", "http://cpbdev/html5tests/pdf/tarifs4.pdf" ];

for (var i = 0, doc; doc = this.docList[i]; ++i) {

// Capture current iteration's file in local scope for the getFile() callback.
(function(f) {

  FS_.root.getFile(getName(doc), {create: true, exclusive: true}, function(fileEntry) {
    fileEntry.createWriter(function(fileWriter) {
      fileWriter.onwriteend = function(e) {
        console.log('Write completed for doc '+i);
      };

      fileWriter.write(GET_REMOTE_FILE(doc));
    }, errorCallback);
  }, errorCallback);
})(doc);
}

readDirectories();
}

function GET_REMOTE_FILE(doc){
  //Impossible to find a way to get File (with possibility to use file.name and others properties)
}

也许通过模拟点击隐藏文件输入(如果可以添加远程文件...)

我希望有人可以帮助我,因为关于远程文件和文件系统的文档很难找到......

谢谢大家:)

布莱斯。

【问题讨论】:

  • 你想做什么?通过文件 API 下载远程文件?
  • 我想将远程文件添加到本地文件系统。 (稍后离线访问)

标签: file html fileapi


【解决方案1】:

我在 HTML5Rocks 上的文章对此进行了深入讨论并给出了a full example。 它涉及 XHR2 + Filesystem API。

【讨论】:

  • 嗨 ebidel,感谢您的链接,这是我需要的,但我有一个关于 CORS 的问题,您说 html5rocks.com 已在其所有页面上启用了 CORS,但是如何将标题放在png 或 pdf 文件?因为当我尝试访问“html5rocks.com/en/tutorials/file/xhr2/access_control_header.png”时,我没有看到图像的 Access-Control-Allow-Origin 标头,并且我无法获取文件......对不起,如果我的问题是微不足道的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-26
  • 1970-01-01
  • 2016-04-30
相关资源
最近更新 更多