0

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

When I try calling my api from postman i get "No data" and status is 401 not found. So the rest method works fine.

I get my excel file if data is found. But I can't seem to handle the 401 response. Angular opens a new window and says site not avaliable: ERR_INVALID_RESPONSE

As you can see im not using http.get cause I want the user to start downloading the excel if the file is found.

Prasanna
  • 303
  • 2
  • 16
Java Gamer
  • 567
  • 3
  • 9
  • 24
  • where is your http get method? – Vikas Apr 10 '18 at 12:34
  • As I mentioned, Im not using it. The point is to download the excel file as a new window is opened – Java Gamer Apr 10 '18 at 12:38
  • @Vikas `As you can see im not using http.get cause I want the user to start downloading the excel if the file is found.`. For the OP, you can use the GET method and request for a blob, then create a file and download it. This would work much more easily. –  Apr 10 '18 at 12:40
  • About the issue of the page opening without the excel file, start returning an error when the file isn't found, instead of always returning a success. –  Apr 10 '18 at 12:41
  • @trichetriche Could you please give me an example of how to do it? Im guessing it will be in the service .ts file? – Java Gamer Apr 10 '18 at 12:44
  • How to do what ? get a blob response ? I don't know how you do it in Java, but google might. For the Angular side, `this.http.get(URL, {responseType: 'blob'})`. –  Apr 10 '18 at 12:51
  • I think the problem is on the angular side, tried searching on the web, it seems they are doing the same thing. but im getting `Argument of type '{ responseType: string; }' is not assignable to parameter of type 'RequestOptionsArgs'. Types of property 'responseType' are incompatible. Type 'string' is not assignable to type 'ResponseContentType'.` – Java Gamer Apr 10 '18 at 13:21
  • https://stackoverflow.com/questions/34149741/how-to-receive-blob-responses-using-angulars-2-angular-http-module/39690808 I'm using the get method, but im getting `Argument of type '{ responseType: string; }' is not assignable to parameter of type 'RequestOptionsArgs'` – Java Gamer Apr 10 '18 at 13:24

0 Answers0