小编use*_*854的帖子

Angular 5下载带有帖子请求的excel文件

我正面临一个问题,我用Angular 1下载了一个Excel文件,但是如果我在Angular 5中实现了相同的代码,则会显示您的文件已损坏的错误.我的回复是在ArrayBuffer中,我无法读取该文件.

以下是我的代码:

服务:

 DownloadData(model:requiredParams):Observable<any>{
  const headers = new Headers();
  const requestOptions = new RequestOptions({ headers: headers });
  requestOptions.headers.append('Content-Type', 'application/json');

   const body = JSON.stringify(model);
  return this.http.post(url, body, requestOptions)
  .map((res:any) => res)
 .catch((e: any) => Observable.throw(this.errorHandler(e)));
 }
Run Code Online (Sandbox Code Playgroud)

零件:

exportToExcel() {
    this.loadingOverlayFlag = true;
   this.podashboardService.DownloadData(this.data).subscribe(result=>{
    console.log(result);
    this.downloadFile(result._body,'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'export.xlsx');
  })
  }

 downloadFile(blob: any, type: string, filename: string) {

 var binaryData = [];
   binaryData.push(blob);

     const url = window.URL.createObjectURL(new Blob(binaryData, {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})); // <-- work with blob directly

     // create hidden dom …
Run Code Online (Sandbox Code Playgroud)

angular-services angular

8
推荐指数
2
解决办法
8485
查看次数

标签 统计

angular ×1

angular-services ×1