javascriptfilesaver.jsbrowser-nativefs

FileSaver.js -.saveAs: How to add listeners


My code (I would have made this runnable except for the filesaver.js dependency (https://github.com/eligrey/FileSaver.js/))

  var array = new Array();
  array[0] = 'a'; //.replace(/<br>/g, '\n\t');
  var blob = new Blob(array, {type: "text/plain;charset=utf-8"});

  var pub = function () {
    console.log('hello');
    console.log(new Error().stack);
  }

    let loc = window.location.pathname.substring(window.location.pathname.lastIndexOf("/") + 1);
    let obj = window.saveAs(blob, loc);
    obj.onwriteend = pub;
//    obj.onerror = pub;
    obj.onabort = pub;

There is no issue with the file saving

The issues are that the onwriteend handler gets called before I've even picked a file, and onabort never gets called. I don't know what's going on here. I'm trying to get a callback when the content is written to disk or when the file chooser is aborted.


Solution

  • FileSaver.js is limited by its use of the save link tunnel: It's declared listeners do not work, and there is no way to get the name and directory of saved file, or even to know if the file was saved at all.

    Using browser-nativefs instead of FileSaver.js is good in that it uses established FileHandler / File API. But browser-nativefs is too limited for my purposes: You cannot get the directory / URI / URL of the file download. The only way to provide this "link" is through the (FileSave.js) browser file download process outside of the scope of what JavaScript can see.