java 如何使用 GWT 客户端下载文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4020949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:25:48  来源:igfitidea点击:

How to download a file using GWT client?

javagwtservlets

提问by DG.

What is the best way to download a pdf file using GWT client ? Should I invoke a normal servlet to do that ? or is there a different preferred approach to handle this problem ?

使用 GWT 客户端下载 pdf 文件的最佳方法是什么?我应该调用一个普通的 servlet 来做到这一点吗?或者是否有不同的首选方法来处理这个问题?

I am new to GWT, so if some sample code would be of great help.

我是 GWT 的新手,所以如果一些示例代码会有很大帮助。

Thanks Deep

谢谢深

回答by cupakob

Try it with GET...

用 GET 试试...

Window.open(GWT.getHostPageBaseURL() + "FileRepository/doDownload?docId=" + dokument.getId(), "", "");

回答by Italo Borssatto

You can implement a Servlet download the file OR you can do that using Data URIs:

您可以实现 Servlet 下载文件,或者您可以使用数据 URI执行此操作:

  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 URIwith the file content received or generate the data content.
  3. Use Window.opento open a file save dialog passing the formatted DataURI.
  1. 使您的 GWT RPC 方法返回文件内容或数据以生成文件。
  2. 在客户端,使用接收到的文件内容格式化数据 URI或生成数据内容。
  3. 使用Window.open打开一个文件保存对话框经过格式化DataURI

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

看看这个参考资料,了解数据 URI 的用法:

Export to csv in jQuery

在 jQuery 中导出到 csv

回答by Sagar Varpe

the best way is to navigate your browser to that file

最好的方法是将浏览器导航到该文件

on download button add click handler:

在下载按钮上添加点击处理程序:

Button downloadButton = new Button("Download");
downloadButton.addClickHandler(new ClickHandler() {
    @Override
    public void onClick(ClickEvent event) {
        Window.open("url_of_file", "download File", "");            
    }
});