Search code examples
javascriptangularblob

Angular download base64 file data


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 ?


Solution

  • 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," , "")