Chrome.fileSystem store data in file

156 views Asked by At

I am trying to save data collected from the Chrome geolocation api into a txt file using the chrome.fileSystem api but while I am able to create a file for these purposes, the file remains empty at the end of the process. For simplicity, I tried first adding a string (1234567890) to the file. As you can see in the code, i've added several console.log to investigate what lines in the code do not execute and it seems that the code isn't executed from writableFileEntry.creatrwriter() down.

chrome.fileSystem.chooseEntry({type: 'saveFile'}, function(writableFileEntry) {
   console.log('writableFileEntry - here');
    writableFileEntry.createWriter(function(writer) {
      console.log('After writableFileEntry - here');
      writer.onerror = errorHandler;
      writer.onwriteend = function(e) {
        console.log('write complete');
      };
       console.log('before Blob - here');
      writer.write(new Blob(['1234567890'], {type: 'text/plain'}));
    }, errorHandler);
});

The manifest.json file looks like the following:

"permissions": ["location","geolocation", "storage", "unlimitedStorage", "fileSystem", "syncFileSystem", {"fileSystem": ["write"]}
          ],
  "app": {
    "background": {
      "scripts": ["background.js"]
        }
      },
  "sockets": {
        "tcp": {
            "connect": "localhost:80"
        },
        "udp": {
             "send": "localhost:80"
        }
      }

Any guidance or advice would be great! Thanks Ofer

1

There are 1 answers

0
ezero On

I've just had the same issue after using some code from the google documentation. The code refers to a function called errorHandler which does not exist. Add a function called errorHandler() and it should work.

Something like this:

chrome.fileSystem.chooseEntry({type: 'saveFile'}, function(writableFileEntry) {

        function errorHandler(){
            console.log('error');
        };

        writableFileEntry.createWriter(function(writer) {
          writer.onerror = errorHandler;
          writer.onwriteend = function(e) {
            console.log('write complete');
          };
          writer.write(new Blob(['1234567890'], {type: 'text/plain'}));
        }, errorHandler);

    });