Set an excel file as response in REST Service

13,033

application/octet-stream is defined as "arbitrary binary data" in RFC 2046, so given that the file is an excel one change response.header("Content-Type","application/octet-stream"); for response.header("Content-Type","application/vnd.ms-excel");

take a look at this answer about octet stream mime type, might be useful, and this one about excel mime type.

Share:
13,033
Srini
Author by

Srini

Updated on July 28, 2022

Comments

  • Srini
    Srini almost 2 years

    I used the below example to test. REST Api works, but it gives result in stream format. But,I expect result with file option. I used this example to ingrate with Angular JS framework.

    @GET
      @Path("/get")
      @Produces("application/vnd.ms-excel")
      public Response getFile() {
        LOG.info("get - Function begins from here");
    
        File file = new File("/home/test/file.xls");
    
        ResponseBuilder response = Response.ok((Object) file);
        response.header("Content-Disposition",
          "attachment; filename=new-excel-file.xls");
        response.header("Content-Type","application/octet-stream");
        return response.build();
    
      }
    
  • Srini
    Srini over 9 years
    Yes, thanks for quick answer. I have removed response.header("Content-Type","application/octet-stream"); and tried,but still it does not give file save dialog box.
  • th3morg
    th3morg over 9 years
    @user1702169 I believe the behavior of how the file is handled is by browser, not by something you can set on the server side of an API.
  • Jon Hanna
    Jon Hanna over 9 years
    @th3morg sure you can, just set the Content-Disposition header, as in the first answer this answer links to.