0

I need to implement file download .I don't want give any server side file urls to download directly .I created a servlet which will open the file and write it to stream of response.Now coming to front gwt i have onResponseReceived(Request request, Response response) which will be called on receiving the response .Now how to proceed further? .My operation required is ,file in stream should be downloaded to client computer.

can one help me regarding this ?

Bhaswanth
  • 283
  • 6
  • 14

3 Answers3

2

Did you try Window.open(ServletUrl, "_parent", "location=no")?

And try setting the ContentType in the response to "application/exe"

This will prompt user to save or run.

Servlet Code:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String filename = URLDecoder.decode(request.getPathInfo(), "UTF-8");
    File file = new File("/path/to/files", filename);
    response.setContentType("application/x-download");
    response.setHeader("Content-Disposition", "attachment; filename=" + filename);
    response.setHeader("Content-Length", file.length());

    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try {
        input = new BufferedInputStream(new FileInputStream(file));
        output = new BufferedOutputStream(response.getOutputStream());

        byte[] buffer = new byte[8192];
        for (int length = 0; (length = input.read(buffer)) > 0;) {
            output.write(buffer, 0, length);
        }
    } finally {
        if (output != null) try { output.close(); } catch (IOException ignore) {}
        if (input != null) try { input.close(); } catch (IOException ignore) {}
    }
}
Yusuf K.
  • 4,195
  • 1
  • 33
  • 69
  • thanks for the reply ..but i dnt wanna display url to user..if i do this..it will open a new window with proper url ..i dnt wanna do tht ...i wanna do something in gwt ..which will make http req to servlet and get the object and user shud be able to download ..like what we see in rapidshare right..ofcourse that is too complex..but i want in similar fashion . – Bhaswanth Dec 15 '10 at 12:27
  • @Bhaswanth Please try this method. Because with this method you will give user a servlet location not a file location. Servlet will write file to client. And because of contenttype no browser window will be opened only seen save, run, cancel prompt dialog. – Yusuf K. Dec 15 '10 at 13:30
  • ya..this is fine..but i would like to know how that can be done ..how can i trigger the download automatically in the code ... – Bhaswanth Dec 15 '10 at 14:18
  • This is a type of triggering the download, you send file to client with stream and user starts to downloading. I don't understand what you really wants. Isn't This method like rapidshare? – Yusuf K. Dec 15 '10 at 14:45
  • ok...fine..if i use this..its downloading file with my servlet name ..but no extension ...it want it to be download by exact name with extension ..how can this be done ? – Bhaswanth Dec 15 '10 at 15:11
  • This must be easy. I'll update the answer look it again :) In this case user will see the full filename – Yusuf K. Dec 15 '10 at 15:24
  • thanq for the solution :) wat i actually intended was programmatically call the download servlet..here a new browser window will be opened rite..i dnt wanna do tht .nothing should be appeared except download dialog box .that is wat i was looking for . – Bhaswanth Dec 16 '10 at 04:35
  • ok, I have updated the answer as your needs. See window.open line I typed "_parent" to second paramater. – Yusuf K. Dec 16 '10 at 06:53
2

You could use any of _blank, _parent, _top, _self

  • "_blank" attribute causes the "target" of the hyperlink to open in a new
  • "_top" attribute causes the "target" of the hyperlink to display at the top level of all currently defined framesets.
  • "_parent" attribute causes the "target" of the hyperlink to display in the entire area of the current frameset.
  • "_self" attribute causes the "target" of the hyperlink to open in the current frame.

Source

0

You can do that without the servlet, using just GWT RPC and Data URIs:

  1. Make your GWT RPC method return the file content or the data to generate the file.
  2. On the client side, format a Data URI with the file content received or generate the data content.
  3. Use Window.open to open a file save dialog passing the formatted DataURI.

Take a look at this reference, to understand the Data URI usage:

Export to csv in jQuery

Community
  • 1
  • 1
Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88