Rest 角度2触发器excel下载
当后端已经在响应中提供下载时,如何在angular 2中触发下载 我的后端提供如下API端点:Rest 角度2触发器excel下载,rest,angular,http,typescript,download,Rest,Angular,Http,Typescript,Download,当后端已经在响应中提供下载时,如何在angular 2中触发下载 我的后端提供如下API端点: Workbook userExcelReport = excelReportGenerator.createUserExcelReport(userSurveys); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setH
Workbook userExcelReport = excelReportGenerator.createUserExcelReport(userSurveys);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=\"" + userFileName + ".xlsx\"");
try {
userExcelReport.write(response.getOutputStream());
userExcelReport.close();
response.flushBuffer();
return new ResponseEntity(HttpStatus.OK);
} catch (IOException e) {
logger.error("The excel workbook could not write to the outputStream ", e);
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
这种方法被证明是有效的,(因为进入后端(没有前端)会正确触发下载),但现在我想制作一个角度为2的页面来触发下载
我已经准备好了页面,我在服务中有:
return this._http.get(this.url, {
search: params
});
但是现在我不知道如何使用响应触发下载(不知道应该如何映射它或什么)一个非常简单的解决方案:如果端点已经触发下载,只需构建端点url(使用其查询参数) 然后重定向到那里
window.location.href = this.url;
this.url = environment.apiURL;
一个非常简单的解决方案:如果端点已经触发下载,只需构建端点url(及其查询参数) 然后重定向到那里
window.location.href = this.url;
this.url = environment.apiURL;
您可以直接链接到excel资源URL。或者使用window.location使浏览器转到该url并下载文件。无需使用AJAX请求。可能与@toskv重复,但这是关于创建文件的问题,这个问题是关于触发后端响应实体的下载。-.http.get()会触发下载。您的问题应该是:如何显示保存对话框来保存http.get()下载的响应,这就是复制的全部内容。http.get不会触发下载您可以直接链接到excel资源URL。或者使用window.location使浏览器转到该url并下载文件。无需使用AJAX请求。可能与@toskv重复,但这是关于创建文件的问题,这个问题是关于触发后端响应实体的下载。-.http.get()会触发下载。您的问题应该是:如何显示保存对话框来保存http.get()下载的响应,这就是复制的全部内容。http.get不会触发下载