2

I am using gwt canvas .

I am having 74kb string(image) data which I want to pass it to servlet. So that servlet process that data and throw the contents to the browser . In this way it will prompt user to download it.

From client side I am using RequestBuilder to call a servlet ,set request data to it ,data is large so I am using post request. It is hitting servlet also throwing contents on the browser but not showing anything downloading.

The current url having canvas .I thinks that's why it is not downloading anything this conclusion is because If am opening that servlet directly using http://localhost:8080/servlet then its downloading it property (In this case i am not proving any content from client side) but for the url having canvas its giving problem.

So is there any way where i can open a url in new tab and can call servlet using post request in gwt.

pbhle
  • 2,856
  • 13
  • 33
  • 40
  • but from client side he is calling POST and on server first he is having GET method implemented then in servlet he gets file content as stream and throwing that cotents only. My case is different from that. – pbhle Sep 25 '13 at 11:33

1 Answers1

3

You can use a Form Panel for do the upload, the Form Panel will use a hidden iframe,

FormPanel form = new FormPanel();
form.setMethod(FormPanel.METHOD_POST);
form.setAction("/downloadServlet");
FlowPanel hiddenPanel = new FlowPanel();
hiddenPanel.add(new Hidden("name1", "value"));
hiddenPanel.add(new Hidden("name2", "value"));
form.setWidget(hiddenPanel);
RootPanel.get().add(form);
form.submit();

The content return by the servlet if you put the correct header will be downloaded by the user navigator.

public class ServletDownloadDemo extends HttpServlet{

  private static final int BYTES_DOWNLOAD = 1024;

  @Override
  public void doPost(HttpServletRequest request, 
   HttpServletResponse response) throws IOException{
    //Get Parameters
    String name1 = request.getParameter("name1");
    String name2 = request.getParameter("name2");

    response.setContentType("text/plain");
    response.setHeader("Content-Disposition",
                     "attachment;filename=downloadname.txt");
    ServletContext ctx = getServletContext();
    InputStream is = ctx.getResourceAsStream("/testing.txt");

    int read=0;
    byte[] bytes = new byte[BYTES_DOWNLOAD];
    OutputStream os = response.getOutputStream();

    while((read = is.read(bytes))!= -1){
        os.write(bytes, 0, read);
    }
    os.flush();
    os.close(); 
   }
}
  • I want to send file contents from client to server... where do I get contents from POST request.. – pbhle Sep 25 '13 at 09:26
  • 1
    The Hidden widget is a and you can put here your file content. In the servlet, you use the request.getParameter("name1") for retreive your data, process it and return it to the client with corrects headers – Patrice De Saint Steban Sep 25 '13 at 11:55