簡體   English   中英

如何在Angular 7中使用Blob下載任何文件類型(dgn,docx等)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM