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文档"
希望您的后端应该喜欢如下
let file = fs.readFileSync(tmpFilePath)
res.setHeader('Content-disposition', `attachment; filename=temp_file.jpg;status=OK`)
res.send(file)
Angular Service
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');
}