4 回答

TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊
如前所述,FileSystem和File API以及FileWriter API可用于從瀏覽器選項(xiàng)卡/窗口的上下文讀取和寫(xiě)入文件到客戶端計(jì)算機(jī)。
有一些與FileSystem和FileWriter API有關(guān)的事情,你應(yīng)該知道,其中一些被提到,但值得重復(fù):
API的實(shí)現(xiàn)目前僅存在于基于Chromium的瀏覽器(Chrome和Opera)中
這兩個(gè)API均于2014年4月24日從W3C標(biāo)準(zhǔn)軌道中刪除,截至目前為專有
從未來(lái)實(shí)現(xiàn)瀏覽器中刪除(現(xiàn)在的專有)API是可能的
甲沙箱(在磁盤(pán)上以外的位置,其中文件可以產(chǎn)生沒(méi)有影響)用于存儲(chǔ)與所述的API所創(chuàng)建的文件
使用虛擬文件系統(tǒng)(磁盤(pán)上不一定存在的目錄結(jié)構(gòu)與從瀏覽器中訪問(wèn)時(shí)所使用的形式相同)表示使用API創(chuàng)建的文件
以下是如何直接和間接使用API來(lái)執(zhí)行以下操作的簡(jiǎn)單示例:
寫(xiě)文件:
bakedGoods.set({ data: [{key: "testFile", value: "Hello world!", dataFormat: "text/plain"}], storageTypes: ["fileSystem"], options: {fileSystem:{storageType: Window.PERSISTENT}}, complete: function(byStorageTypeStoredItemRangeDataObj, byStorageTypeErrorObj){}});
閱讀文件:
bakedGoods.get({ data: ["testFile"], storageTypes: ["fileSystem"], options: {fileSystem:{storageType: Window.PERSISTENT}}, complete: function(resultDataObj, byStorageTypeErrorObj){}});
使用原始文件,F(xiàn)ileWriter和FileSystem API
寫(xiě)文件:
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);
閱讀文件:
function onQuotaRequestSuccess(grantedQuota){ function getfile(directoryEntry) { function readFile(fileEntry) { function read(file) { var fileReader = new FileReader(); fileReader.onload = function(){var fileData = fileReader.result}; fileReader.readAsText(file); } fileEntry.file(read); } directoryEntry.getFile( "testFile", {create: false}, readFile ); } requestFileSystem(Window.PERSISTENT, grantedQuota, getFile);}var desiredQuota = 1024 * 1024 * 1024;var quotaManagementObj = navigator.webkitPersistentStorage;quotaManagementObj.requestQuota(desiredQuota, onQuotaRequestSuccess);
雖然FileSystem和FileWriter API不再符合標(biāo)準(zhǔn),但在我看來(lái),在某些情況下,它們的使用是合理的,因?yàn)椋?/p>
來(lái)自未實(shí)現(xiàn)的瀏覽器供應(yīng)商的更新興趣可能會(huì)將它們重新置于其上
實(shí)施(基于Chromium)瀏覽器的市場(chǎng)滲透率很高
谷歌(Chromium的主要貢獻(xiàn)者)尚未給出API的生命終止日期
但是,“某些情況”是否包含您自己的情況,由您決定。
* BakedGoods由這個(gè)人維持正確:)

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊
如果用戶選擇文件<input type="file">
,您可以使用File API 讀取和處理該文件。
設(shè)計(jì)不允許讀取或?qū)懭肴我馕募_@違反了沙箱。來(lái)自維基百科 - > Javascript - >安全性:
JavaScript和DOM為惡意作者提供了通過(guò)Web在客戶端計(jì)算機(jī)上運(yùn)行腳本的潛力。瀏覽器作者使用兩個(gè)限制包含此風(fēng)險(xiǎn)。首先,腳本在沙箱中運(yùn)行,在沙箱中,腳本只能執(zhí)行與Web相關(guān)的操作,而不能執(zhí)行創(chuàng)建文件等通用編程任務(wù) 。
2016 UPDATE:直接訪問(wèn)文件系統(tǒng)經(jīng)由能夠文件系統(tǒng)API,這是僅由鉻和歌劇支持和可能最終沒(méi)有被實(shí)現(xiàn)為通過(guò)其他瀏覽器(與邊緣的例外)。有關(guān)詳細(xì)信息,請(qǐng)參閱Kevin的回答。
添加回答
舉報(bào)