【问题标题】:chrome.fileSystem api file to temp directorychrome.fileSystem api 文件到临时目录
【发布时间】:2016-03-14 13:09:02
【问题描述】:

如何在 chrome 文件系统 api 中将文件写入临时目录?

        chrome.fileSystem.getWritableEntry("temp dir here", function(entry) {
            var uniqid = Math.floor((Math.random() * 10) + 1)+Date.now();
            entry.getFile(uniqid+'.txt', {create:true}, function(entry) {
                entry.createWriter(function(writer) {
                    writer.write(new Blob([printText], {type: 'text/plain'}));
                });
            });
        });

我需要这个独特的.txt 来写入临时目录,然后获取该位置..

【问题讨论】:

  • 你需要它做什么?要写入真实的文件系统,您需要用户的同意,您不能只选择一个位置。如果你只需要一个临时文件,你可以用虚拟文件系统来做。
  • 如何使用虚拟文件系统?我需要在临时文件夹中创建文件,然后复制到另一个文件夹。复杂,但需要这样:(
  • 问题是,您需要用户可以访问它,还是只需要在应用程序内部访问它? (另外,我想扩展标签不适用,因为它是一个仅限应用程序的 API)
  • 我的应用程序将创建文件到临时文件夹,然后将其复制到第二个(我的自定义位置),然后第三方软件会将其打印到热敏打印机,因为它会监视其中的新文件第二个位置并打印它们。这就是重点。这是 chrome 应用程序,而不是扩展程序。

标签: javascript google-chrome google-chrome-app html5-filesystem


【解决方案1】:

在将 HTML5 虚拟文件系统复制到最终的“真实”文件夹之前,您可以将其用作“暂存”位置。您仍然需要请求访问最终文件夹。

大部分细节可以在Exploring the FileSystem API文章中找到,但思路如下:chrome.fileSystem API 是建立在相同的原理上的,当你用chrome.fileSystem.chooseEntry请求它们时你会得到不同的条目

要获得虚拟文件系统而不是真实文件系统,您需要一个不同的调用:

window.webkitRequestFileSystem(
  window.TEMPORARY,
  5*1024*1024 /* 5MB, adjust as needed, may require "unlimitedStorage" permission */,
  onInitFs,
  errorHandler
);

function onInitFs(filesystem) {
  /* do something with filesystem.root, which is a DirectoryEntry,
     just as you would with `chrome.fileSystem.chooseEntry` */
}

如需更多文档,MDN 是个好地方。

是的,有“不要使用”的大红色可怕警告。它不便携,因为只有 Chrome 实现了它,而其他浏览器决定不这样做。但是对于专门编写一个 Chrome 应用程序来说很好。如果您需要确保 Chrome 将继续支持它 - 好吧,chrome.fileSystem 与它紧密结合。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 2016-10-29
    • 2010-11-10
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多