Javascript 如何在Angular 7中使用blob下载任何文件类型(dgn、docx等)
我正在从API返回字节数组。 我的要求是下载所有文件类型。下载dgn文件或其他类型时出现错误(无法加载PDF文档) 我能够将字节数组转换为blob并下载PDF文件Javascript 如何在Angular 7中使用blob下载任何文件类型(dgn、docx等),javascript,angular,Javascript,Angular,我正在从API返回字节数组。 我的要求是下载所有文件类型。下载dgn文件或其他类型时出现错误(无法加载PDF文档) 我能够将字节数组转换为blob并下载PDF文件 download(): void { this._service.download().subscribe(data => { this.downloadFile(this.byteToBlob(data, 'application/octet-stream', 512));
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)
角度服务
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');
}