Getting FileError when trying to use the HTML5 file api in google chrome

ⅰ亾dé卋堺 提交于 2019-12-07 12:13:01

问题


I was trying to create a file using the file system API in chrome. Initially I tried PERSISTENT storage as follows

window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
webkitStorageInfo.requestQuota(webkitStorageInfo.PERSISTENT, 1024*1024, 
    function(grantedBytes) {
        window.requestFileSystem(webkitStorageInfo.PERSISTENT, grantedBytes, onInitFs, 
        errorHandler);
    }, 
    errorHandler);

It was working fine initially. But now when I try the same code is giving me the following error

NOT_SUPPORTED_ERR: DOM Exception 9

Then I tried TEMPORARY file storage as follows

window.requestFileSystem(TEMPORARY, 1024*1024, onInitFs, errorHandler);

That is giving me some FileError with the code 2 which means Security Error. Can anyone please help me on these issues?


回答1:


Are you trying to place the code in a page that is stored on your local computer and run it with Chrome? Did you add the flag

--allow-file-access-from-files

in your Chrome shortcut? I happened to encounter the same error as yours and suddenly I realized that I forgot to add this flag to run my chrome.




回答2:


I can't reproduce your error. I made a simple extension that uses the filesystem API in a content script and it works fine for me. Do you get any errors on your console? If not, build off this code and see if you get any more errors.

manifest.json:

{
  "name": "Testing FS",
  "manifest_version":2,
  "version": "1.0",
  "content_scripts": [
    {
      "matches":["*://*.google.com/*"],
      "js":["script.js"]
    }
  ]
}

script.js:

webkitStorageInfo.requestQuota(webkitStorageInfo.TEMPORARY, 1024*1024, function(grantedBytes) {
    console.log("bytes granted: ", grantedBytes);
    window.webkitRequestFileSystem(webkitStorageInfo.TEMPORARY, grantedBytes, onInitFs, errorHandler)
}, errorHandler);

function errorHandler(e) {
  console.log("FS error", e);
}

function onInitFs(fs) {
  fs.root.getFile('log.txt', {create: true}, function(fileEntry) {
      console.log(fileEntry);
  }, errorHandler);
}


来源:https://stackoverflow.com/questions/10477218/getting-fileerror-when-trying-to-use-the-html5-file-api-in-google-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!