javascript - chrome.fileSystem api 文件到临时目录

标签 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) {
                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/

相关文章:

javascript - 捕获 <webview> 中由 HTML 标签引起的 "Failed to load resources"错误

javascript - 为什么我的单选按钮的文本偏离中心

javascript - 如果轮播显示的元素较少,则隐藏自定义导航按钮 - Owl Carousel

jquery - 为什么 Google Chrome 通过 jQuery 有不同的 console.log() 输出?

javascript - 有没有办法在 google chrome 扩展程序中增加变量而无需打开扩展程序?

javascript - setTimeout 在 Chrome 中似乎不起作用

javascript - 应用程序后台脚本,访问打开窗口的元素

ssh - Chromebook SSH 客户端出现 "NaCl plugin exited with status code 255"错误?

javascript - 在 native Javascript 中查找具有类的元素的索引

javascript - 如何使用 recorder.js、getUserMedia 和 blob.slice 正确切片音频文件?