file download using RequestBuilder of gwt

微笑、不失礼 提交于 2020-01-25 21:26:10

问题


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 ?


回答1:


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) {}
    }
}



回答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




回答3:


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



来源:https://stackoverflow.com/questions/4448840/file-download-using-requestbuilder-of-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!