如前所述,文件API以及FileWriter和FileSystem API可用于从浏览器选项卡/窗口的上下文中在客户端机器上存储文件。
然而,关于后两个api,有几件事你应该知道:
Implementations of the APIs currently exist only in Chromium-based browsers (Chrome & Opera)
Both of the APIs were taken off of the W3C standards track on April 24, 2014, and as of now are proprietary
Removal of the (now proprietary) APIs from implementing browsers in the future is a possibility
A sandbox (a location on disk outside of which files can produce no effect) is used to store the files created with the APIs
A virtual file system (a directory structure which does not necessarily exist on disk in the same form that it does when accessed from within the browser) is used represent the files created with the APIs
下面是一些简单的例子,说明了如何直接或间接地使用api来实现这一点:
烘焙食品*
bakedGoods.get({
data: ["testFile"],
storageTypes: ["fileSystem"],
options: {fileSystem:{storageType: Window.PERSISTENT}},
complete: function(resultDataObj, byStorageTypeErrorObj){}
});
使用原始的文件、FileWriter和文件系统api
function onQuotaRequestSuccess(grantedQuota)
{
function saveFile(directoryEntry)
{
function createFileWriter(fileEntry)
{
function write(fileWriter)
{
var dataBlob = new Blob(["Hello world!"], {type: "text/plain"});
fileWriter.write(dataBlob);
}
fileEntry.createWriter(write);
}
directoryEntry.getFile(
"testFile",
{create: true, exclusive: true},
createFileWriter
);
}
requestFileSystem(Window.PERSISTENT, grantedQuota, saveFile);
}
var desiredQuota = 1024 * 1024 * 1024;
var quotaManagementObj = navigator.webkitPersistentStorage;
quotaManagementObj.requestQuota(desiredQuota, onQuotaRequestSuccess);
尽管FileSystem和FileWriter api已经不在标准轨道上,但在某些情况下,它们的使用是合理的,在我看来,因为:
未实现的浏览器供应商重新燃起的兴趣可能会让他们重新回到这个平台上
实现(基于chromium的)浏览器的市场渗透率很高
谷歌(Chromium的主要贡献者)没有给出api的生命终止日期
然而,“某些情况”是否包括你自己的情况,由你自己决定。
*BakedGoods是由这里的这个家伙维护的:)