Search code examples
javascriptgoogle-chrome-apphtml5-filesystem

Writing to a file from a Chrome packaged app


Below is the code with which I am trying to write to a simple text file from a Chrome App.

Once createWriter is invoked, nothing happens. Can't get it working, any inputs on this is appreciated.

chrome.fileSystem.chooseEntry({type: 'saveFile'}, 
 function(writableFileEntry) {
  writableFileEntry.createWriter(function(writer) {
    writer.onwriteend = function(e){
      console.log("save completed!!");
    };
    writer.onerror = function(e){
        console.log("save failed!!");
    };
    writer.write(new Blob(["Hello World!!!"],{type: 'text/plain'})); 
  }, errorHandler);
});

Mainifest.json

{
 "name": "FileIO",
 "version": "1.0",
 "manifest_version": 2,
 "minimum_chrome_version": "23",
 "app": {
 "background": {
 "scripts": ["background.js", "myscript.js"]
 }
 },
 "icons": {
 "128": "icon.png"
 },
 "permissions": [
 "fileSystem",
  {"fileSystem" : ["write","retainEntries", "directory"] }
 ],
 "file_handlers": {
    "text": {
      "types": [
          "text/*"
      ]
    }
  }
}

Solution

  • errorHandler function was defined out of scope and was causing the issue, moving the errorHandler function inside the scope fixed the issue! Thanks to Xan for helping with troubleshooting.