filesystem:
协议存储参考相同来源的文件document
其中要求LocalFileSystem
。也就是说,如果问题处的 JavaScript 是在以下位置创建的:http://example.org
,路径为LocalFileSystem
应该同源http://example.org
, not file:
协议。
如果您尝试存储文件或文件夹以供访问file:
协议,离线,您可以创建一个.html
用作模板书签的文档。
参观当地.html
在线归档一次以获取文件并填充LocalFileSystem
. If navigator.onLine
is true
, 导航http://example.org
,否则获取并处理存储在的文件和文件夹LocalFileSystem
.
创建一个列表作为JSON
或 JavaScriptArray
存储要获取的文件列表,而不是解析.html
document
文件位置。
将本地文件存储为书签。启动 Chromium、Chrome--allow-file-access-from-files
标志设置为访问filesystem:
协议来自file:
协议和file:
协议在filesystem:
协议,如果不在线。
<!DOCTYPE html>
<html>
<head>
<title>LocalFileSystem Offline Videos Bookmark</title>
</head>
<body>
<script>
// location to visit if online
const onLineURL = "https://lorempixel.com/"
+ window.innerWidth
+ "/"
+ window.innerHeight + "/cats";
const props = {
requestedBytes: 1024 * 1024 * 20000,
folder: "videos",
// list of files to fetch for offline viewing
mediaList: [
"http://mirrors.creativecommons.org/movingimages/webm/"
+ "ScienceCommonsJesseDylan_240p.webm"
, "https://nickdesaulniers.github.io/netfix/demo/frag_bunny.mp4"
]
};
let grantedBytes = 0;
function getLocalFileSystem ({requestedBytes = 0, mediaList=[], folder = ""}) {
if (!requestedBytes || !mediaList.length || !folder) {
throw new Error("requestedBytes: Number"
+ " or mediaList: Array"
+ " or folder: String not defined");
};
// do stuff with `filesystem:` URL
function processLocalFilePath(localPath) {
const video = document.createElement("video");
document.body.appendChild(video);
video.controls = true;
video.src = localPath;
}
function errorHandler(err) {
console.log(err);
}
function writeFile(dir, fn, fp, localPath) {
console.log(dir, fn, fp, localPath);
dir.getFile(fn, {}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
// do stuff when file is written
console.log(e.type, localPath + " written");
window.webkitResolveLocalFileSystemURL(localPath
, function(file) {
// file exists in LocalFileSystem
processLocalFilePath(localPath);
}, errorHandler)
};
fileWriter.onerror = errorHandler;
fetch(fp).then(function(response) {
return response.blob()
}).then(function(blob) {
fileWriter.write(blob);
}).catch(errorHandler)
}, errorHandler);
}, errorHandler);
}
if (mediaList && mediaList.length) {
navigator.webkitTemporaryStorage.requestQuota(requestedBytes
, function(grantedBytes_) {
grantedBytes = grantedBytes_;
console.log("Requested bytes:", requestedBytes
, "Granted bytes:", grantedBytes);
window.webkitRequestFileSystem(window.TEMPORARY
, grantedBytes
, function(fs) {
const url = fs.root.toURL();
mediaList.forEach(function(filename) {
const localPath = url + folder + "/"
+ filename.split("/").pop();
window.webkitResolveLocalFileSystemURL(localPath
, function(file) {
// file exists in LocalFileSystem
console.log(localPath + " exists at LocalFileSystem");
processLocalFilePath(localPath)
}, function(err) {
console.log(err, localPath
+ " not found in LocalFileSystem");
// Exception is thrown if file
// or folder path not found
// create `folder` directory, get files
fs.root.getDirectory(folder, {}
, function(dir) {
writeFile(dir
, filename.split("/").pop()
, filename
, localPath);
}),
errorHandler
})
})
})
}, errorHandler)
}
}
if (location.href !== onLineURL && navigator.onLine) {
location.href = onLineURL;
} else {
getLocalFileSystem(props);
}
</script>
</body>
</html>
也可以看看
如何在 file: 协议中使用 webkitRequestFileSystem https://stackoverflow.com/questions/37502091/how-to-use-webkitrequestfilesystem-at-file-protocol
如何使用javascript打印文件夹内的所有txt文件 https://stackoverflow.com/questions/37634049/how-to-print-all-the-txt-files-inside-a-folder-using-java-script/
用JS读取本地XML https://stackoverflow.com/questions/41279589/read-local-xml-with-js/
如何使用JavaScript写入文件(用户目录)? https://stackoverflow.com/questions/36098129/how-to-write-in-file-user-directory-using-javascript/
另一种方法可能是利用ServiceWorker
将 Service Worker 和 Offline 添加到您的 Web 应用程序中 https://developers.google.com/web/fundamentals/getting-started/codelabs/offline/
Service Worker 示例:自定义离线页面示例 https://googlechrome.github.io/samples/service-worker/custom-offline-page/