1

I've got this code:

   let file: File = fileList[0];
   console.log(file);

   var fileReader = new FileReader();
   fileReader.onload = (event) => {
       this.fileString = fileReader.result as string;
   };
   fileReader.readAsText(file);

That saves a file's blob as a string. Now I need code that can recover the original blob from the fileString. I'm not seeing any posts that address this already.

Does anyone know how I can recover the original blob?

Currently, I'm trying this code (because the file I'm uploading it a word doc):

     var myblob = new Blob([this.fileString], {
            type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
     });
     console.log(myblob);
     saveAs(myblob, "test.docx");

And I'm surprised to see that the original file when console logged is size: 247583 and when I console log in the 2nd section, it's size 433474

Kevin Dunn
  • 13
  • 3
  • Do you mean something like: `new Blob([this.fileString], { type: 'text/plain' });` where the type is your desired mime type? – r3dst0rm Nov 25 '19 at 14:49
  • Yeah, I think that's part of the puzzle for me. I updated my original post to show how I'm currently trying to do to save the word doc. – Kevin Dunn Nov 25 '19 at 15:00
  • I feel like my issue has something to do with the encoding the occurs when I call readAsText. Like there isn't corresponding decoding when I create a new blob? But I'm not sure if that's correct, and so far I've been unsuccessful at decoding. – Kevin Dunn Nov 25 '19 at 15:07
  • Maybe this could be helpful: https://stackoverflow.com/questions/23024460/javascript-i-created-a-blob-from-a-string-how-do-i-get-the-string-back-out – Andrei Gătej Nov 25 '19 at 16:23

0 Answers0