簡體   English   中英

Angular 7 下載 blob 時文件損壞

[英]Angular 7 get corrupted file while downloading blob

Using Angular 7, i am calling api by posting the url file and try to download it by using 'saveAs' function from the fileSaver library. 文件正在下載,但無法打開,因為它已損壞

我的電話如下:

var file_url = (response as any).headers['Location'] + 'files/Data.xlsx';
var filename = 'Data_' + this.getDateService.getDateFile() + '.xlsx';

const httpOptions = {
          headers: new HttpHeaders({
            'Content-Type': 'application/x-www-form-urlencoded'
          }),
          responseType: 'arraybuffer',
          observe: 'response'
        };

let downloadParameters = { filename: 'Data_' + this.getDateService.getDateFile() + '.xlsx', file: file_url }

this.downloadFileService.downloadFile(downloadParameters, httpOptions).subscribe(reponse => {

          var blob = new Blob([(response as any).body], { type: 'application/vnd.openxmlformat-officedocument.spreadsheetml.sheet' });
          saveAs(blob, filename);
})

我嘗試了什么

  • 通過application/octet-stream切換類型 MIME application/vnd.openxmlformat-officedocument.spreadsheetml.sheet
  • 通過blob或 blob 將 responseType arraybuffer切換為 json

下面,來自服務的響應標頭

在此處輸入圖像描述

該文件存在於響應正文中

在此處輸入圖像描述

你們有什么線索嗎?

試試這樣:

this.downloadFileService.downloadFile(downloadParameters, { responseType: 'blob' }).subscribe(blob=> {
  saveAs(blob, filename);
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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