Angular 7在下载Blob时遭到损坏的文件 [英] Angular 7 get corrupted file while downloading blob

查看:255
本文介绍了Angular 7在下载Blob时遭到损坏的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Angular 7,我通过发布url文件来调用api,并尝试通过使用fileSaver库中的'saveAs'函数来下载它. 该文件正在下载中,但由于损坏而无法打开.

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. The file is downloading but it cannot be opened because it's corrupted.

我的电话是:

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作为json
  • 切换responseType arraybuffer

下面是服务的响应标头:

文件存在于响应正文中:

你们有什么线索吗?

推荐答案

尝试如下:

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

这篇关于Angular 7在下载Blob时遭到损坏的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆