Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How to create Download Button

    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.

    #2
    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.

    Comment


      #3
      RPCManager can not be used because we use JSON as the server side communication mechanism. Moreover, the downloadable file size is usually bigger than 4kB . How could you overcome these issues with the above solutions ?

      Comment


        #4
        Neither concern is relevant.. Perhaps take a closer look at the suggestions already given.

        Comment


          #5
          Originally posted by Isomorphic
          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.
          Hi, I tries to do it as you suggested via a DMI method.

          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();
              }
          However, I end up with an exception. What do I do wrong?

          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)
          thanks,
          Zdary

          Comment


            #6
            Don't create a new RPCManager. If you declare an argument of type RPCManager, the current (already created) RPCManager will be passed to you.

            Comment


              #7
              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();
                  }
              *.ds.xml
              Code:
              	<operationBindings> 
              		<operationBinding operationType="downloadFile">
              			<serverObject className="cz.bcom.FilesDMI" methodName="download" />
              		</operationBinding>
              	</operationBindings>
              and GUI
              Code:
              DataSource filesDS = DataSource.get("filesDS");
              downloadButton.addClickHandler(new ClickHandler() {  
                public void onClick(ClickEvent event) {
                  filesDS.downloadFile(fileGrid.getSelectedRecord());
                }  
              });

              Comment

              Working...
              X