I am trying to write to local file system using FileSystem API in Chrome. I am getting the following error while executing
FileError is deprecated. Please use the 'name' or 'message' attributes of DOMError rather than 'code'.
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
var fs = null;
function errorHandler(e) {
alert("hi");
var msg = '';
switch (e.code) { //Error is being reported here
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;
}
}
function onInitFs(fs) {
alert("hi");
fs.root.getFile('log.txt', {create: true}, function(fileEntry) {
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
console.log('Write completed.');
};
fileWriter.onerror = function(e) {
console.log('Write failed: ' + e.toString());
};
// Create a new Blob and write it to log.txt.
var blob = new Blob(['Add some text'], {type: 'text/plain'});
fileWriter.write(blob);
}, errorHandler);
}, errorHandler);
}
function initFS(){
window.requestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler);
}
if (window.requestFileSystem) {
initFS();
}
And since i know the execution goes to errorHandler method i assume i am doing something wrong while calling the requestFileSystem() method. I would really appreciate help here. Thanks in advance
Further research shows that it fails with security error. How do i basically bypass that
解决方案
Seems like your errorHandler wasn't doing anything with msg anyway. Try this:
function errorHandler(e) { console.log("ERROR!: name: [" + e.name + "] message: [" + e.message + "]"); }