Am从API返回字节数组。 我的要求是下载所有文件类型。下载dgn文件或其他类型时出现错误(无法加载PDF文档)
能够将字节数组转换为blob并下载PDF文件。
download(): void {
this._service.download().subscribe(data => {
this.downloadFile(this.byteToBlob(data, 'application/octet-stream', 512));
},
error => console.log("Error downloading the file."),
() => console.log('Completed file download.'));
}
downloadFile(data: any): void {
const blob = new Blob([data], { type: 'application/pdf' });
const url = window.URL.createObjectURL(blob);
window.open(url);
}
在下载非PDF文件时出现错误“无法加载PDF文档”
答案 0 :(得分:0)
希望您的后端应该如下
let file = fs.readFileSync(tmpFilePath)
res.setHeader('Content-disposition', `attachment; filename=temp_file.jpg;status=OK`)
res.send(file)
角度服务
downloadMedia(): Observable<any> {
return this.http.get<any>(`/api/event/medias/download/image`, {responseType: 'blob' as 'json'})
}
组件
this.service.downloadMedia().subscribe(result => {
let imageFormat = result.type.toString()
saveAs(result, 'temp_file.jpg');
}