【问题标题】:Post call on button click to download excel report发布呼叫按钮单击以下载 Excel 报告
【发布时间】:2019-01-29 07:24:11
【问题描述】:

我正在尝试下载 excel 文件,在 Angular js 中单击按钮时提供 POST 调用。 我的 java 控制器返回 void 。

写入excel文件的服务:

response.setContentType("application/ms-excel");
response.setHeader("Content-Disposition","attachment filename=\"" + fileName + ".xls"+ "\"");
workbook.write(response.getOutputStream());

响应的类型为HttpServletResponse

我如何在Angular中写方法来下载文件??

【问题讨论】:

  • 我们要发帖而不是href

标签: java angular rest spring-boot


【解决方案1】:

从服务器下载文件。

点击按钮调用函数。

 downloadFile():void
  {
    this.getFiles("http://localhost:80080/api/demo/GetTestFile")
    .subscribe(fileData => 
      {
      let b:any = new Blob([fileData], { type: 'application/zip' });
      var url= window.URL.createObjectURL(b);
        window.open(url);
      }
    );
  }

  public getFiles(path: string):Observable<any>{
    let requestOption = new RequestOptions({responseType: ResponseContentType.Blob});
    return this.http.get(filePath, requestOption)
        .map((response: Response) => <Blob>response.blob())  ;
  }

【讨论】:

  • 我们正在使用 Angular js
  • 这是 Angular 2+。
  • 知道如何在 angularjs 中做到这一点吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-24
  • 1970-01-01
  • 1970-01-01
  • 2011-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多