Angular 下载base64文件数据
Angular download base64 file data
当我尝试在 Angular 上保存具有这种格式的 base64 文件时:
// 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();
这段代码给我一个损坏的文件,我该怎么办?
在 Angular 中(通常)我使用 file-saver :
import { saveAs } from 'file-saver';
const blob = new Blob([receivedFile], {type: receivedFile.infos.type});
FileSaver.saveAs(blob, receivedFile.infos.name);
此外,尝试删除字符串的 data:image/jpeg;base64,
部分:
receivedFile.file.replace("data:image/jpeg;base64," , "")
解决这个问题:
- 已从 receivedFile.file 中删除
data:image/jpeg;base64
。
- 使用函数
atob()
,const byteCharacters = atob(receivedFile.file);
- 然后制作blob
const blob = new Blob([byteCharacters], {type: this.receivedFile.infos.type});
当我尝试在 Angular 上保存具有这种格式的 base64 文件时:
// 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();
这段代码给我一个损坏的文件,我该怎么办?
在 Angular 中(通常)我使用 file-saver :
import { saveAs } from 'file-saver';
const blob = new Blob([receivedFile], {type: receivedFile.infos.type});
FileSaver.saveAs(blob, receivedFile.infos.name);
此外,尝试删除字符串的 data:image/jpeg;base64,
部分:
receivedFile.file.replace("data:image/jpeg;base64," , "")
解决这个问题:
- 已从 receivedFile.file 中删除
data:image/jpeg;base64
。 - 使用函数
atob()
,const byteCharacters = atob(receivedFile.file);
- 然后制作blob
const blob = new Blob([byteCharacters], {type: this.receivedFile.infos.type});