0

I'm using the following code to read data from local storage on the client side:

fileChange(event) 
{
    let file = event.target.files[0];
    var reader = new FileReader();
    reader.readAsText (file);
    reader.onload = () => {
      console.log (reader.result);
    }
    
    let fileSaverService = new FileSaverService; 
    var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
    fileSaverService.save (file, "hello.txt");

The contents in 'file' is saved to "hello.txt" This is not what I need. I want to modify the contents of the selected file (event.target.files[0]).

Thank you in advance, Zvika

Zvi Vered
  • 459
  • 2
  • 8
  • 16
  • Modify `reader.result` at onload callback. Create a blob from modified result as you do with "Hello, world!" and pass that blob to `fileSaverService.save ` method. Do it all in the `onload` callback. – Eldar Dec 03 '19 at 17:38
  • Thank you very much. It seems "chrome" always saves to 'Downloads'. Is this a chrome feature or a bug in my code ? – Zvi Vered Dec 03 '19 at 17:49
  • Well you can not control where to save data with javascript. You can do what browser allows you. Try setting chrome's download folder manually and save your file, see where it saves. – Eldar Dec 03 '19 at 17:51
  • It works. Best regards. – Zvi Vered Dec 03 '19 at 18:29

0 Answers0