2

I've created an app for Google Chrome that stores a file into the File System. I can read this file but every time I try to export it, it doesn't work. I've tried some methods:

but they doesn't work.

Replacement for fileEntry.toURL() in Chrome Packaged Apps talks about my problem.. So I changed my code into

function readFileRdf() {
    window.requestFileSystem(window.PERSISTENT, 5*1024*1024, function(filesystem) {
      fs = filesystem;
      fs.root.getFile('rdf.txt', {create: false}, function(fileEntry) {
      // Get a File object representing the file,
      // then use FileReader to read its contents.
        fileEntry.file(function(file) {
          var reader = new FileReader();
          reader.onloadend = function(e) {
            fromFileSystemRdf = e.target.result;
            arr = fromFileSystemRdf;
            exportRdf(arr);
          };
          reader.readAsText(file);
        }, errorHandler);
      }, errorHandler);
});
}

and

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

the last problem is that I get an error with createWriter

Error in response to fileSystem.chooseEntry: TypeError: Cannot call method 'createWriter' of undefined

Up.. adding a console.log(chrome.runtime.lastError); it says

Object {message: "Invalid calling page. This function can't be called from a background page."}

Question solved. Since is not possible to call a method from a background page, I've used this solution: (Remember, Google Chrome doesn't accept inline javascript)

button for exporting is pressed

`document.getElementById("button2").addEventListener("click",readFileRdf);`

method readFileRdf creates a new page:

function readFileRdf() {
    chrome.app.window.create("data.html");
}

data.html calls the data.js file

 <!DOCTYPE html>
    <html>
          <head>
             <meta charset="utf-8">
             <title>Data page</title>
          </head>

          <body>
          </body>

          <script rel="text/javascript" src="data.js"></script>  
        </html>

and data.js allows the user to read the file from filesystem and download it

window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;

function initFs() {
  window.requestFileSystem(window.PERSISTENT, 5*1024*1024, function(filesystem) {
      fs = filesystem;
      readFile(fs);
   }, errorHandler);
}

function readFile(fs) {
    fs.root.getFile('rdf.txt', {create: false}, function(fileEntry) {
      // Get a File object representing the file,
      // then use FileReader to read its contents.
        fileEntry.file(function(file) {
          var reader = new FileReader();
          reader.onloadend = function(e) {
            fromFileSystemRdf = e.target.result;
            arr = fromFileSystemRdf;
            exportRdf(arr);
          };
          reader.readAsText(file);
        }, errorHandler);
      }, errorHandler);
}

function exportRdf(arr){
  console.log(arr);
  chrome.fileSystem.chooseEntry({type: 'saveFile'}, function(writableFileEntry) {
    console.log(chrome.runtime.lastError);
    writableFileEntry.createWriter(function(writer) {
      writer.onerror = errorHandler;
      writer.onwriteend = function(e) {
        console.log('write complete');
      };
      writer.write(new Blob([arr], {type: 'text/plain'}));  
    }, errorHandler);
});
}

initFs();
Community
  • 1
  • 1
user2971827
  • 76
  • 1
  • 5

0 Answers0