Angular service.ts:
getExcel() {
let link: string = (this.url);
return link;
}
component.ts:
public getExcel() {
//checks if string is empty, undefined or not null
if (this.langCode) {
return window.open(this.avrs.getExcel());
}
}
Java rest.java
@GET
@Path("/excel")
@Produces("application/vnd.ms-excel")
public Response getTransportCostsExcel(
@Context HttpServletRequest request,
) {
byte[] excelInBytes = excelGen.getExcel();
if(excelInBytes == null){
return Response.status(Response.Status.NOT_FOUND).entity("No data").build();
}
//Header details
String contentType = "application/vnd.ms-excel";
Response.ResponseBuilder responseBuilder = javax.ws.rs.core.Response.ok((Object) excelInBytes);
responseBuilder.type(contentType);
responseBuilder.header("Content-Disposition", "attachment; filename=" + fileName);
//Returns Excel
return responseBuilder.build();
}
当我尝试从邮递员调用我的api时,我得到“没有数据”,状态是401未找到。所以其余方法工作正常。
如果找到数据,我会获取我的excel文件。但我似乎无法处理401响应。 Angular打开一个新窗口说 网站不可用:ERR_INVALID_RESPONSE
正如您所看到我不使用http.get因为我希望用户在找到该文件时开始下载excel。