如何显示GWT客户端文件,而不是下载: [英] How to show file in GWT client side, instead of Downloading:
问题描述
我需要一点帮助实现这一点,在我的应用程序,用户可以将文件上传到服务器及其存储为Blob对象,而现在我需要向他们展示在有要求的用户了。
I need little help to achieve this, in my app user can upload files to server and its stored as blob object, and now i need to show them to user up on there request.
我起来就是显示在下面code,
What I am up to is show in below code,
在服务器端,我把内容回应:
(这code基于这个博客帖子的 WaterTalks )
On server side I put content to response: (This code is implemented based on this blog post WaterTalks)
resp.setContentType("text/plain");
resp.setHeader("Content-Disposition", "attachment; filename=output.txt");
PrintWriter out = resp.getWriter();
out.println("This is the output content");
out.println("Probably something dynamic should go in here:::::");
PersistenceManager pm = null;
try {
pm = PMF.get().getPersistenceManager();
javax.jdo.Transaction transaction = pm.currentTransaction();
Extent e = pm.getExtent(WaterTalkFiles.class, true);
Iterator iter = e.iterator();
String returns = "";
WaterTalkFiles file = (WaterTalkFiles)iter.next();
Blob blob = file.getData();
byte[] buffer = blob.getBytes();
String s = new String(buffer);
out.println(s);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != pm)
pm.close();
}
现在在客户端,当用户点击显示按钮,我想显示在浏览器中的文件内容,而不是下载。
Now in client side when user click show button i want to show the file content in browser, not to download it.
我的客户端code是:
My client side code is:
showfilecontentButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
String link = "/FileUploadByWaterTalks";
container.add(new HTML("<a href=\"" + link + "\">ShowFile</a>"));
}
});
在code以上(客户端code)不显示文件,它只是下载文件的内容。
但我不希望用户下载的话,我想向他们展示了它的内容。
The code above (Client side code) not showing content of file its just downloading the file. But I don't want user to download it, I want show them the content of it.
和,我必须配置一些在这里去解决它。
And, do I have to configure something over here to work it out.
resp.setContentType("text/plain");
resp.setHeader("Content-Disposition", "attachment; filename=output.txt");
希望你得到了我的问题是什么。请自由分享自己的想法和解决方案,以实现这一目标。
Hope you got what my problem is. Please be free to share your thoughts and solutions to achieve this.
感谢。
截至上的第一个答案,这里的基地,我改变了我的code的某些部分:
Up on the bases of first answer here, I changed some portion of my code:
更新code部分是:
resp.setHeader("Pragma", "no-cache");
final String url = "http://127.0.0.1:8888/FileUploadByWaterTalks";
String name = "output.txt";
Anchor link1 = new Anchor(name);
RootPanel.get().add(link1);
link1.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Frame f = new Frame(url);
f.setSize("600px", "400px");
f.getElement().getStyle().setBorderWidth(0, Unit.PX);
RootPanel.get().add(f);
}
});
但仍然是浏览器要求我将文件保存而不是显示在浏览器本身吧。
But still the browser asking me to save the file instead of showing it in the browser itself.
推荐答案
首先拆下内容处置在你的servlet。
First remove the 'Content-Disposition' in your servlet.
二,在你的code使用GWT 锚
键,当用户点击打开一个新窗口或iframe中的链接。
Second, use a GWT Anchor
in your code and when the user clicks open the link in a new window or an iframe.
在这里,你有一个例如使用新的窗口,另一个使用的iframe:
Here you have a example using new window, and another using iframe:
final String url = "http://gwtquery.googlecode.com/git/README.txt";
String name = "README.txt";
Anchor link1 = new Anchor(name);
RootPanel.get().add(link1);
link1.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Window.open(url, "_blank", "");
}
});
Anchor link2 = new Anchor(name);
RootPanel.get().add(link2);
link2.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Frame f = new Frame(url);
f.setSize("600px", "400px");
f.getElement().getStyle().setBorderWidth(0, Unit.PX);
RootPanel.get().add(f);
}
});
本方法适用于该浏览器能够显示,但是要确保你发送相应的内容类型
头(文/平原,文本/ HTML的任何文件,图像/ PNG等)
This approach works for any file which the browser is capable to display, but be sure that you send the appropriate Content-Type
header (text/plain, text/html, image/png etc.)
这篇关于如何显示GWT客户端文件,而不是下载:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!