Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 7在下载blob时获取损坏的文件_Angular_Typescript_Download_Httpclient_Filesaver.js - Fatal编程技术网

Angular 7在下载blob时获取损坏的文件

Angular 7在下载blob时获取损坏的文件,angular,typescript,download,httpclient,filesaver.js,Angular,Typescript,Download,Httpclient,Filesaver.js,使用Angular 7,我通过发布url文件来调用api,并尝试通过使用fileSaver库中的“saveAs”函数来下载它。 文件正在下载,但无法打开,因为它已损坏 我的电话如下: var file_url = (response as any).headers['Location'] + 'files/Data.xlsx'; var filename = 'Data_' + this.getDateService.getDateFile() + '.xlsx'; const httpOpt

使用Angular 7,我通过发布url文件来调用api,并尝试通过使用fileSaver库中的“saveAs”函数来下载它。 文件正在下载,但无法打开,因为它已损坏

我的电话如下:

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);
})
我尝试的是:

  • 按应用程序/八位字节流切换MIME application/vnd.openxmlformat-officedocument.spreadsheetml.sheet类型
  • 将responseType arraybuffer按blob或blob切换为json
下面是来自服务的响应标头:

该文件位于响应正文中:

你们有什么线索吗

试着这样做:

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

哇,谢谢你,它真的起作用了。为什么它在没有httpHeaders的情况下工作?这里是http头。我没有存储在另一个变量中。如果你也投票给我,那就太好了:)我写的是“回复”而不是“回复”。通过纠正这一点,它正在发挥作用。