javascript 为什么这个文件系统 api requestQuota 调用失败?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16487803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does this filesystem api requestQuota call fail?
提问by Don Rhummy
I'm writing an HTML5 app to run in Chrome but it will be on the local filesystem (so they'll start it by double-clicking an html file). It is throwing an error when I try to access the filesystem and I think it's because it's a local file. Is there a way to make Chrome allow this?
我正在编写一个 HTML5 应用程序以在 Chrome 中运行,但它将位于本地文件系统上(因此他们将通过双击一个 html 文件来启动它)。当我尝试访问文件系统时抛出错误,我认为这是因为它是本地文件。有没有办法让 Chrome 允许这样做?
(NOTE: I do get the popup asking me to allow the app to store permanantly and I click "OK". It still throws this error)
(注意:我确实收到了弹出窗口,要求我允许应用程序永久存储,然后单击“确定”。它仍然会引发此错误)
The below code throws the error:
下面的代码抛出错误:
DOMException {message: "NotSupportedError: DOM Exception 9", name: "NotSupportedError", code: 9, INDEX_SIZE_ERR: 1, DOMSTRING_SIZE_ERR: 2…}
DOMException {message: "NotSupportedError: DOM Exception 9", name: "NotSupportedError", code: 9, INDEX_SIZE_ERR: 1, DOMSTRING_SIZE_ERR: 2…}
filetest.html
文件测试.html
<!DOCTYPE html>
<html>
<head></head>
<body>
<script>
//File System handler
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
function onInitFs(fs) {
console.log('Opened file system: ' + fs.name);
}
function errorHandler(e) {
var msg = '';
switch (e.code) {
case FileError.QUOTA_EXCEEDED_ERR:
msg = 'QUOTA_EXCEEDED_ERR';
break;
case FileError.NOT_FOUND_ERR:
msg = 'NOT_FOUND_ERR';
break;
case FileError.SECURITY_ERR:
msg = 'SECURITY_ERR';
break;
case FileError.INVALID_MODIFICATION_ERR:
msg = 'INVALID_MODIFICATION_ERR';
break;
case FileError.INVALID_STATE_ERR:
msg = 'INVALID_STATE_ERR';
break;
default:
msg = 'Unknown Error';
break;
};
console.log('Error: ' + msg);
}
/** THIS CAUSES IT TO THROW AN ERROR */
window.webkitStorageInfo.requestQuota(window.PERSISTENT, 5*1024*1024, function(grantedBytes) {
window.requestFileSystem(window.PERSISTENT, grantedBytes, onInitFs, errorHandler);
}, function(e) {
console.log('Error', e);
});
</script>
</body>
</html>
If I instead change it to ask for temporary storage, it still throws an error, but now it's a SECURITY_ERR
:
如果我改为将其更改为请求临时存储,它仍然会引发错误,但现在是SECURITY_ERR
:
window.requestFileSystem(window.TEMPORARY, 5*1024*1024, onInitFs, errorHandler);
回答by Don Rhummy
Not sure this is the best answer but it appears to be a security restriction on local files. Starting Chrome as below fixes the issue:
不确定这是最好的答案,但它似乎是对本地文件的安全限制。如下启动 Chrome 解决了这个问题:
google-chrome --allow-file-access-from-files
That will allow creating persistent storage.
这将允许创建持久存储。
回答by Andrew Willems
If your app requires the user to double-click on an html file, then your answer might be the only way to go. However, if the need is to access a local file, but you have some flexibility in terms of howto access that local file, then consider creating a small local server.
如果您的应用程序要求用户双击 html 文件,那么您的答案可能是唯一的方法。但是,如果需要访问本地文件,但您在如何访问该本地文件方面具有一定的灵活性,则可以考虑创建一个小型本地服务器。
On Windows, install http-server (npm install -g http-server
) and run http-server
from your project directory. On Mac/Linux, run python -m SimpleHTTPServer
from your local directory. In the browser, access the locally hosted web site. On Windows I had to use localhost:8080
while on the Mac I had to use localhost:8000
.
在 Windows 上,安装 http-server ( npm install -g http-server
) 并http-server
从您的项目目录运行。在 Mac/Linux 上,python -m SimpleHTTPServer
从本地目录运行。在浏览器中,访问本地托管的网站。在 Windows 上我必须使用,localhost:8080
而在 Mac 上我必须使用localhost:8000
.
All credit for this answer goes to @orszaczky who gave this answer to another SO question. That answer also discusses why this is a security issue, and why using the --allow-file-access-from-files
flag is potentially dangerous.
这个答案的所有功劳都归功于@orszaczky,他为另一个 SO 问题提供了这个答案。该答案还讨论了为什么这是一个安全问题,以及为什么使用该--allow-file-access-from-files
标志具有潜在危险。
By the way, this is not only an issue for Chrome (v49.0) but also for Opera (v35.0), on both Windows and Mac.
顺便说一下,这不仅是 Chrome (v49.0) 的问题,也是 Windows 和 Mac 上 Opera (v35.0) 的问题。