chrome.fileSystem api文件到临时目录

chrome.fileSystem api file to temp directory

本文关键字:fileSystem api 文件 chrome      更新时间:2023-09-26

如何在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来写入临时目录,然后获得那个位置。。

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

大多数细节可以在探索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与它紧密相连