Skip to content Skip to sidebar Skip to footer

Receive An Excel File As Response In Javascript From A Rest Service

I am calling a webservice call from javascript to get an excel file as response from java appl from server. I am kind of stuck how to get the response and access it. Here in the we

Solution 1:

In JAX-RS, for excel file, annotate the method with @Produces("application/vnd.ms-excel") :

1.Put @Produces(“application/vnd.ms-excel”) on service method.

2.Set “Content-Disposition” in Response header to prompt a download box.

The code:

import java.io.File;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

@Path("/excel")
public class ExcelService {

    private static final String FILE_PATH = "c:\\excel-file.xls";

    @GET
    @Path("/get")
    @Produces("application/vnd.ms-excel")
    public Response getFile() {

        File file = new File(FILE_PATH);

        ResponseBuilder response = Response.ok((Object) file);
        response.header("Content-Disposition",
            "attachment; filename=new-excel-file.xls");
        return response.build();

    }

}

I do not know if it is still useful, however you can see


Post a Comment for "Receive An Excel File As Response In Javascript From A Rest Service"