如何将文件写入 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 虚拟文件系统复制到最终的“真实”文件夹之前,您可以使用 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
与它紧密耦合。
关于javascript - chrome.fileSystem api 文件到临时目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34175791/