0

When I try to save on Angular a base64 file with this format :

// receivedFile.file = "data:image/jpeg;base64,/9j/4AAQSkZJR..."

var a = document.createElement('a');
const blob = new Blob([receivedFile.file], {type: receivedFile.infos.type});
a.href = URL.createObjectURL(blob);
a.download = receivedFile.infos.name;
a.click(); 

This code give me a corrupted file, how can I do ?

Galileoomega
  • 121
  • 1
  • 9

2 Answers2

3

In Angular (and in general) I use file-saver :

import { saveAs } from 'file-saver';
const blob = new Blob([receivedFile], {type: receivedFile.infos.type});
FileSaver.saveAs(blob, receivedFile.infos.name);

Also, try to remove the data:image/jpeg;base64, part of the string :

receivedFile.file.replace("data:image/jpeg;base64," , "")
Jeremy Thille
  • 25,196
  • 9
  • 41
  • 59
0

To resolve this problem :

  • Removed data:image/jpeg;base64 from the receivedFile.file.
  • Use the function atob(), const byteCharacters = atob(receivedFile.file);
  • Then make the blob const blob = new Blob([byteCharacters], {type: this.receivedFile.infos.type});
Galileoomega
  • 121
  • 1
  • 9