Is there any way to create a "Download" button in smartgwt ? Our application generates a dynamic File/OutputStream in server side. When the user clicks on a particular Button or Link that files needs to be downloaded through browser.
Announcement
Collapse
No announcement yet.
X
-
If a GET request is sufficient, just redirect the page to the target URL (the equivalent of window.location.replace()).
If you instead want to do a normal server-side operation that results in a file download, you can do it as a DMI and just use the HttpServletResponse to send back the file, just call RPCManager.doCustomResponse() to signal to the server that you're doing this.
-
Originally posted by IsomorphicIf a GET request is sufficient, just redirect the page to the target URL (the equivalent of window.location.replace()).
If you instead want to do a normal server-side operation that results in a file download, you can do it as a DMI and just use the HttpServletResponse to send back the file, just call RPCManager.doCustomResponse() to signal to the server that you're doing this.
Code:public void download(HttpServletRequest request, HttpServletResponse response) throws Exception { RPCManager manager = new RPCManager(request, response); manager.doCustomResponse(); response.setContentType("text/plain"); response.setHeader("Content-Disposition", "attachment; filename=\"example-data.any\""); OutputStream out = response.getOutputStream(); byte[] utf8Bytes = "content of the file".getBytes("UTF8"); out.write(utf8Bytes); out.flush(); out.close(); }
Code:=== 2011-07-21 19:05:58,499 [l0-8] DEBUG RPCManager - Content type for RPC transaction: text/plain; charset=UTF-8 === 2011-07-21 19:05:58,500 [l0-8] ERROR IDACall4Spring - IDACall - Error executing operation: download java.io.IOException: Error - getWriter() called on request that has already returned an OutputStream - enable debug logging on com.isomorphic.servlet.ProxyHttpServletResponse to see a stack trace of the previous caller. at com.isomorphic.servlet.ProxyHttpServletResponse.getWriter(ProxyHttpServletResponse.java:174) at com.isomorphic.servlet.RequestContext.out(RequestContext.java:368) at com.isomorphic.rpc.RPCManager.completeResponse(RPCManager.java:973) at com.isomorphic.rpc.RPCManager.send(RPCManager.java:582) at com.isomorphic.servlet.IDACall.processRPCTransaction(IDACall.java:156) at com.isomorphic.servlet.IDACall.processRequest(IDACall.java:121) at com.isomorphic.servlet.IDACall.doPost(IDACall.java:73) at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) at com.isomorphic.servlet.BaseServlet.service(BaseServlet.java:152) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1097) at com.isomorphic.servlet.CompressionFilter.doFilter(CompressionFilter.java:259)
Zdary
Comment
-
thank, it works.
My code looks like this
DMI file
Code:public void download(RPCManager manager, HttpServletResponse response) throws Exception { manager.doCustomResponse(); response.setContentType("text/csv"); response.setHeader("Content-Disposition", "attachment; filename=\"example-data.any\""); OutputStream out = response.getOutputStream(); byte[] utf8Bytes = "content of the file".getBytes("UTF8"); out.write(utf8Bytes); out.flush(); out.close(); }
Code:<operationBindings> <operationBinding operationType="downloadFile"> <serverObject className="cz.bcom.FilesDMI" methodName="download" /> </operationBinding> </operationBindings>
Code:DataSource filesDS = DataSource.get("filesDS"); downloadButton.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { filesDS.downloadFile(fileGrid.getSelectedRecord()); } });
Comment
Comment