【问题标题】:How to download .xls file in angular 6 using file saver如何使用文件保护程序以 Angular 6 下载 .xls 文件
【发布时间】:2019-08-26 07:35:27
【问题描述】:

我试图从服务器下载 .xls 文件,但它抛出如下错误:->

错误是:

SyntaxError: Unexpected token � in JSON at JSON.parse 的位置 0 () 在 XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:31219:51) 在 ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:2761:31) 在 Object.onInvokeTask (http://localhost:4200/vendor.js:72221:33) 在 ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:2760:36) 在 Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (http://localhost:4200/polyfills.js:2528:47) 在 ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [作为调用] (http://localhost:4200/polyfills.js:2836:34) 在调用任务 (http://localhost:4200/polyfills.js:3880:14) 在 XMLHttpRequest.globalZoneAwareCallback (http://localhost:4200/polyfills.js:3906:17) 文字:“��ࡱ�;��”

在我的组件中,我实现了调用服务的函数,并且我正在使用文件保护程序来保存文件,但是它抛出错误。

download.component.ts:->

import * as FileSaver from 'file-saver';
     export_licence(){
        this.service.export_licence().subscribe((file: Blob) => {
          FileSaver.saveAs(file,'degreeCertificate.xls');
        }, (err) => {
          console.log("Download Error:", err);
        });
      }

download.service.ts :->

export_licence() {
  return this.http.post(this.export_licence_url,{responseType: "blob"});
}

我应该能够从服务器下载 .xls 文件,任何帮助将不胜感激。

【问题讨论】:

标签: javascript angular6


【解决方案1】:

检查响应头设置是否正确(对于内容类型),并确保文件没有损坏。

似乎重复downloading xlsx file in angular 2 with blob

【讨论】:

    【解决方案2】:

    这对我有用

    download.component.ts:->

    import * as FileSaver from 'file-saver';
         export_licence(){
            this.service.export_licence().subscribe((file: Blob) => {
              FileSaver.saveAs(file,'degreeCertificate.xls');
            }, (err) => {
              console.log("Download Error:", err);
            });
          }
    

    在服务中我正确添加了标题。

    download.service.ts:->

    export_licence() {
      let headers = new Headers({ 
        'Content-Type':'application/json', 
        'Accept': 'application/.xls'
     });
     let options = { headers : headers };
      return this.http.post(this.export_licence_url,options,{responseType: "blob"});
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 2017-05-20
      • 1970-01-01
      相关资源
      最近更新 更多