I have an ExportServlet that generates XLSX (Excel) files that my user will request from my GWT application by clicking the export button.
If I use the GET approach, the user is prompted to download the file. The code is as follows:
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { byte[] xlsx = createTest(""); sendXLSX(resp, xlsx, "test.xlsx"); } catch (Exception e) { e.printStackTrace(); } } void sendXLSX(HttpServletResponse response, byte[] bytes, String name) throws IOException { ServletOutputStream stream = null; stream = response.getOutputStream(); response.setContentType(CONTENT_TYPE_XLSX); response.addHeader("Content-Type", CONTENT_TYPE_XLSX); response.addHeader("Content-Disposition", "inline; filename=" + name); response.setContentLength((int) bytes.length); stream.write(bytes); stream.close(); }
This is called by the GWT client as follows:
String url = GWT.getModuleBaseURL() + "ExportServlet"; Window.open(url, "", "");
and the user will be prompted to download the file. Ok, that is what I want :)
But I would like to add a lot of data to the request, and afaik, there is a limit to how much data you can put in the URL parameter ("ExportServlet? Data = ..."), so I would, for example, wrap this in a POST request.
I tried the following from GWT:
String url = GWT.getModuleBaseURL() + "ExportServlet"; RequestBuilder builder = new RequestBuilder( RequestBuilder.POST, url); Request response = builder.sendRequest("test data", new RequestCallback() { @Override public void onResponseReceived(Request request, Response response) { System.out.println(""); } @Override public void onError(Request request, Throwable exception) { System.out.println(""); } });
and this is in my servlet:
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { String data = req.getReader().readLine(); byte[] xlsx = createTest(data); sendXLSX(resp, xlsx, "test.xlsx"); } catch (Exception e) { e.printStackTrace(); } }
but the user is not prompted to download the file. The doPost method is called and the data is received by the servlet, but should I extract the XLSX file from the response that I get in the GWT application? and how can i do this?
Appreciate any help or comments :)