为什么这个文件系统api请求定额调用失败? [英] Why does this filesystem api requestQuota call fail?
问题描述
我正在编写HTML5应用程序以在Chrome中运行,但它将位于本地文件系统上(因此它们将通过双击html文件启动它)。当我尝试访问文件系统时发生错误,我认为这是因为它是本地文件。有没有办法让Chrome允许这样做?
(注意:我弹出窗口要求我允许应用程序永久存储,然后单击确定。仍然抛出这个错误)
下面的代码会抛出错误:
DOMException {消息:NotSupportedError:DOM Exception 9,名称:NotSupportedError,代码:9,INDEX_SIZE_ERR:1,DOMSTRING_SIZE_ERR:2 ...}
filetest.html
<!DOCTYPE html>
< html>
< head>< / head>
< body>
< script>
//文件系统处理程序
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
函数onInitFs(fs){
console.log('打开的文件系统:'+ fs.name);
}
函数errorHandler(e){
var msg ='';
switch(e.code){
case FileError.QUOTA_EXCEEDED_ERR:
msg ='QUOTA_EXCEEDED_ERR';
休息;
case FileError.NOT_FOUND_ERR:
msg ='NOT_FOUND_ERR';
休息;
case FileError.SECURITY_ERR:
msg ='SECURITY_ERR';
休息;
case FileError.INVALID_MODIFICATION_ERR:
msg ='INVALID_MODIFICATION_ERR';
休息;
case FileError.INVALID_STATE_ERR:
msg ='INVALID_STATE_ERR';
休息;
默认值:
msg ='未知错误';
休息;
};
console.log('Error:'+ msg);
}
/ **这会导致错误发生* /
window.webkitStorageInfo.requestQuota(window.PERSISTENT,5 * 1024 * 1024,function(grantedBytes){
window.requestFileSystem(window.PERSISTENT,grantedBytes,onInitFs,errorHandler);
},函数(e){
console.log('Error',e);
} );
< / script>
< / body>
< / html>
如果我改变它来请求临时存储,它仍然会抛出一个错误,但现在它是一个 SECURITY_ERR
:
window.requestFileSystem(window.TEMPORARY,5 * 1024 * 1024,onInitFs,errorHandler);
不确定这是最好的答案,但它似乎成为本地文件的安全限制。如下所示启动Chrome可解决问题:
google-chrome - 允许文件从文件访问
这将允许创建持久性存储。
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?
(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…}
filetest.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
:
window.requestFileSystem(window.TEMPORARY, 5*1024*1024, onInitFs, errorHandler);
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:
google-chrome --allow-file-access-from-files
That will allow creating persistent storage.
这篇关于为什么这个文件系统api请求定额调用失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!