![](/img/trans.png)
[英]How to open a preview file (pdf, docx, txt, etc.) in another page of the browser using Angular and Java
[英]How to download any file types(dgn,docx etc) using blob in Angular 7
我正在從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)
角度服務
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');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.