Javascript chrome.fileSystem api文件到临时目录

Javascript chrome.fileSystem api文件到临时目录,javascript,google-chrome,google-chrome-app,html5-filesystem,Javascript,Google Chrome,Google Chrome App,Html5 Filesystem,如何将文件写入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)

如何将文件写入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
来写入temp dir,然后获取该位置。

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

大部分细节可以在文章中找到,但其思想如下:
chrome.fileSystem
API是基于相同的原则构建的,当您使用
chrome.fileSystem.chooseSentry
API请求它们时,您只会得到不同的条目

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

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` */
}
对于更多文档,这是一个好地方


是的,有红色的警告“请勿使用”。它不可移植,因为只有Chrome实现了它,其他浏览器决定不这样做。但是专门编写一个Chrome应用程序是可以的。如果您需要保证Chrome将继续支持它,那么,
Chrome.fileSystem
与它紧密耦合。

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