Search code examples
javascriptgoogle-chromegoogle-chrome-extensiongoogle-chrome-appgoogle-nativeclient

Save persistent file in NaCl and read inside app JavaScript code


In my chrome app, I would like to save a persistent file in NaCl module (using Pepper API or nacl_io), then open and read it with JavaScript code inside the app.

The NaCl part is implemented by using nacl_io and works fine (with html5fs and PERSISTENT type).

How can I do the other part in JS code?


Solution

  • You can access the files in JavaScript using the Filesystem API.

    Here is an example of reading a file from that page:

    function onInitFs(fs) {
    
      fs.root.getFile('log.txt', {}, 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) {
             var txtArea = document.createElement('textarea');
             txtArea.value = this.result;
             document.body.appendChild(txtArea);
           };
    
           reader.readAsText(file);
        }, errorHandler);
    
      }, errorHandler);
    
    }
    
    window.requestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler);
    

    In your case, you'll want to read from the PERSISTENT filesystem instead of the TEMPORARY filesystem.

    Please note that this API is prefixed in Chrome as window.webkitRequestFileSystem.