1

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

5
  • In my Chrome that's not an error but a warning and onInitFs() is called (at least i get the alert). Anyway, as the message tells you, you shouldnt use FileError itself; furthermore, the whole FileSystem API you're using is 'abandoned' and works only with Chrome. Instead, use the File API to access local files. Commented Jul 24, 2014 at 10:36
  • @Bewusstsein onInitFs() is not called . It terminates with ErrorHandler method Commented Jul 24, 2014 at 10:40
  • @Bewusstsein While it's true that the FileSystem API is abandoned elsewhere, File API is in no way a replacement. They have very different roles. Commented Jul 24, 2014 at 11:35
  • @Bewusstsein Furthermore, the API is explicitly referenced from Chrome Apps docs as a method of file storage. Commented Jul 24, 2014 at 11:37
  • @user3851662 right, i thought your problem was the warning you're getting. Commented Jul 25, 2014 at 6:55

2 Answers 2

1

If you're accessing the script locally (via file:///) you'll need to launch Chrome with the '--allow-file-access-from-files'-flag like so:

C:\Path\To\Chrome.exe --allow-file-access-from-files

source, source

If you're doing this within a Chrome app you'll need to add fileSystem-permissions to your manifest.json:

{
    "manifest_version": 2,
    "name": "...",
    "version": "...",  
    "minimum_chrome_version": "23",
    "permissions": [
        {
            "fileSystem": ["write"]
        }
    ]
}

source

To remove the warning you're getting simply use e.name and/or e.message instead of e.code:

function errorHandler(e) {
    alert(e.name + ": " + e.message);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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 + "]");  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.