angular - 如何使用角度5下载带有blob函数的文件
问题描述
使用 blob 功能下载任何文件类型
private saveAsBlob(data: any) {
const blob = new Blob([data._body],
const file = new File([blob], 'image.png',
FileSaver.saveAs(file);
}
解决方案
我得到了它,它可以与 url 一起使用:
download(row) {
return this.Http
.get(url, {
responseType: ResponseContentType.Blob,
})
.map(res => {
return {
filename: row.name,
data: res.blob()
};
})
.subscribe(res => {
let url = window.URL.createObjectURL(res.data);
let a = document.createElement('a');
document.body.appendChild(a);
a.setAttribute('style', 'display: none');
a.href = url;
a.download = res.filename;
a.click();
window.URL.revokeObjectURL(url);
a.remove();
});
}
推荐阅读
- node.js - 带有 node-sass、autoprefixer 和 cssnano 的 npm
- android - SwipeRefreshLayout:得到 ACTION_MOVE 事件但没有活动指针 id
- r - 在 R 中删除 2000 年之前的年份
- javascript - Puppeteer - 单击没有输入 ID 的按钮
- symfony - Symfony插入表,所有列外键
- variables - 如何使用 NASM 在引导扇区中定义和使用变量?
- php - API url 中的 PHP 回显
- react-native - React Native Maps onMapReady getMapBoundaries
- django - 仪表板 url 生成唯一 url 以允许未连接的用户查看特定仪表板
- reactjs - ListItem(Material UI组件)中的React Typescript prop错误