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
How to download a file using GWT client?
提问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执行此操作:
- Make your GWT RPC method return the file content or the data to generate the file.
- On the client side, format a Data URIwith the file content received or generate the data content.
- Use
Window.open
to open a file save dialog passing the formatted DataURI.
Take a look at this reference, to understand the Data URIusage:
看看这个参考资料,了解数据 URI 的用法:
回答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", "");
}
});