1

I am writing a service to return a xls file by using the json passed to my service. I am using JAX-RS and WINk. Since the json passed to my service is too complex to be a @QueryParam in the url, so I want to use @POST method instead of @GET.

The question is: If I use @GET, I know I can paste the url in the browser to download the file returned by service, but if I use @POST, how can I download the file returned by the service?

The goal is when user post the request to this service, a window will pop up asking either"OPEN", "Download" or "Cancel".

doranT
  • 167
  • 3
  • 11

1 Answers1

2

The most simple way is to use a HTML form:

<form action="rest/report/users" method="post">
ID: <input type="text" name="id"><br>
<input type="submit">
</form>

And

@Path("/report")
public class ReportResource {

    @Path("users")
    @POST
    @Produces(MediaTypeUtils.MS_EXCEL)
    public Response getUsers(@FormParam("id") String id ) {

        // Build the report and get the instance of java.io.File

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

The works like a charm showing the save dialog in Chrome and IE.

Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • Another way, which in particular I like me, is with JQuery. Look [`jQuery.post()`](http://stackoverflow.com/a/3506018/870248). – Paul Vargas Mar 26 '13 at 15:18