Downloading file from GWT
Last time I was discussed about how to create an app to host in google appspot.com, which provides to upload file and show it up on request. In this post I covered first portion, GWT file uploading and saving it to database as a blob object, in this section I
would like to show how can we download the file using persistence manager factory.
Configuration to be done are discussed in the previous section(Link).
what additionally needed is adding a doGet() method and its defenision, also a client side mechanism to request the file.
STEP 1: add doGet() body in "WaterTalkUploadService.java"
doGet(req,resp){
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();
}
}
STEP 2: Create a WaterTalkFileDownloading.java class
add this to its body
final String url = "/FileUploadByWaterTalks";
String name = "output.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);
}
});
Hope all other configuration explained in uploading section is kept as it was.
Thanks :)
No comments:
Post a Comment