2

I am working on the tutorial for HTML5 FileSystem-API. Basically I would like to write a file to my machine. I am using python's simpleHTTPServer to serve the website. Once I load the webpage, the console doesn't raise an error and I get a message that the write was successful. But I can't find the file anywhere on my machine. Here is my code:

function onInitFs(fs) {

  fs.root.getFile('zz11zzlog.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(['Lorem Ipsum'], {type: 'text/plain'});

      fileWriter.write(blob);

    }, errorHandler);

  }, errorHandler);

}

function errorHandler(e) {
  var msg = '';

  switch (e.code) {
    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;
  };

  console.log('Error: ' + msg);
}
window.webkitRequestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler);
gpanterov
  • 1,365
  • 2
  • 15
  • 25

2 Answers2

2

But I can't find the file anywhere on my machine.

If the write was successful, the file would be written to local filesystem at chrome or chromium configuration directory at Default -> File System directory.

See How to Write in file (user directory) using JavaScript?

Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177
  • On Windows, if I go to my chrome file system directory I see a bunch of folders (named 000, 001, 002... and Origins) but none of them have the txt file I specified. Searching the hard drive also doesn't yield anything – gpanterov May 26 '16 at 00:09
  • @gpanterov _"if I go to my chrome file system directory I see a bunch of folders (named 000, 001, 002... and Origins)"_ Yes, the file name provided at `.getFile()` is not the same as created file at `File System` directory. Though you can retrieve the file by file name using `.getFile("fileName", {})` with empty object as second parameter. Have you opened the files in `000`, `001`, `002` folders? Does one contain text `Lorem Ipsum`? – guest271314 May 26 '16 at 00:15
  • Got it! I was able to read the file through the browser so it must be there. I was assuming that it would store it in the folder with my index files – gpanterov May 26 '16 at 00:20
  • @gpanterov See also https://developers.google.com/web/updates/2011/08/Debugging-the-Filesystem-API , http://stackoverflow.com/questions/11676584/where-does-persistent-file-system-storage-store-with-chrome – guest271314 May 26 '16 at 00:23
0

Call this two line for file download

window.requestFileSystem(window.TEMPORARY, fileSize, function(filesystem){
          setTimeout(function() {
          downloadFs(filesystem,fileName);
          }, time);
       }, errorHandler);

 function downloadFs(fs,fileName) {
      fs.root.getFile(fileName, {create: false}, function(fileEntry) {
           console.log("filesystem:"+fileEntry.toURL()) ;
     window.location.href = fileEntry.toURL();
      }, errorHandler); 
  }
msvinay
  • 41
  • 4