gpt4 book ai didi

java - 调用后单击按钮即可下载 Excel 报告

转载 作者:行者123 更新时间:2023-12-02 10:19:50 24 4
gpt4 key购买 nike

我正在尝试下载 Excel 文件,在 Angular js 中单击按钮时进行 POST 调用。我的 java Controller 返回 void 。

写入Excel文件的服务:

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

响应的类型为HttpServletResponse

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

最佳答案

从服务器下载文件。

单击按钮时调用函数。

 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()) ;
}

关于java - 调用后单击按钮即可下载 Excel 报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54415810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com