Announcement

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

    Download file

    I want to Download a file with GWT.

    Which way can i go?

    The Client-javascript send params to Server (RPC) and should get a file like csv, xml or xls back. Then the client should show the download-dialog to save the data on the client-PC.

    Any hints?

    #2
    RPC will not work. Just have your link point to a url that returns the file. Make sure your servlet/controller does something along the lines of

    Code:
    //if returning an image
    response.setContentType("image/jpeg");
    OutputStream out = response.getOutputStream();
    byte[] image = readImageData(....)
    out.write(image);
    out.flush();
    out.close();

    Comment


      #3
      If you want the browser to show a dialog asking if the response should be saved as a file:

      Code:
      //if returning csv-data
      response.setContentType("text/csv");
      response.setHeader("Content-Disposition", "attachment; filename=\"example-data.csv\"");
      OutputStream out = response.getOutputStream();
      byte[] data = readCsvData(....);
      out.write(data);
      out.flush();
      out.close();
      This will show a dialog asking the user if he wants to save the response as a file named "example-data.csv".
      Last edited by nlotz; 17 Dec 2008, 12:34.

      Comment


        #4
        Originally posted by nlotz
        If you want the browser to show a dialog asking if the response should be saved as a file
        ...
        This will show a dialog asking the user if he wants to save the response as a file named "example-data.csv".
        Dear nlotz, I've tried this way, but having this exception and can't understand where is the error... can you help me please?

        Code:
        java.lang.RuntimeException: Unable to report failure
        	at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doUnexpectedFailure(AbstractRemoteServiceServlet.java:107)
        	at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:67)
        	at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
        	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.handle(ServletHandler.java:362)
        	at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
        	at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
        	at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
        	at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
        	at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
        	at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
        	at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
        	at org.mortbay.jetty.Server.handle(Server.java:324)
        	at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
        	at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
        	at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
        	at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:205)
        	at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
        	at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
        	at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
        Caused by: java.io.IOException: Closed
        	at org.mortbay.jetty.AbstractGenerator$Output.write(AbstractGenerator.java:627)
        	at org.mortbay.jetty.AbstractGenerator$Output.write(AbstractGenerator.java:587)
        	at com.google.gwt.user.server.rpc.RPCServletUtils.writeResponse(RPCServletUtils.java:277)
        	at com.google.gwt.user.server.rpc.RemoteServiceServlet.writeResponse(RemoteServiceServlet.java:315)
        	at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:232)
        	at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
        	... 19 more
        My code is:
        Code:
        public class MyServiceImpl extends RemoteServiceServlet implements MyService
        {
        ...
          public Boolean giveFileToDownload(List<MyClass> myClassList)
          {
              HttpServletResponse response = this.getThreadLocalResponse();
              
              response.setContentType("text/csv");
              response.setHeader("Content-Disposition", "attachment; filename=\"data.csv\"");
              
              try
              {
                  OutputStream out = response.getOutputStream();
                  byte[] data = generateCSV(myClassList).getBytes(); // generateCSV() returns String
                  out.write(data);               // here is where the exception is thrown
                  out.flush();
                  out.close();
                  return true;
              }
              catch(IOException ioEx)
              {
                  ioEx.printStackTrace();            
                  return false;
              }
          }
        ...
        }

        Comment


          #5
          I've changed the line
          Code:
          byte[] data = generateCSV(myClassList).getBytes();
          to
          Code:
          byte[] data = "Test text".getBytes();
          and got the same exception, BUT printing the stack trace on the client from onFailure(Throwable caught) method by invoking "caught.printStackTrace()" gave me strange thing:
          Code:
          com.google.gwt.user.client.rpc.InvocationException: Test text
          	at com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived(RequestCallbackAdapter.java:201)
          	at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:287)
          	at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:393)
          	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
          	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
          	at java.lang.reflect.Method.invoke(Method.java:597)
          	at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
          	at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
          	at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)
          	at com.google.gwt.dev.shell.BrowserChannel.reactToMessagesWhileWaitingForReturn(BrowserChannel.java:1713)
          	at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:165)
          	at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:120)
          	at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:507)
          	at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:264)
          	at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
          	at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
          	at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:188)
          	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
          	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
          	at java.lang.reflect.Method.invoke(Method.java:597)
          	at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
          	at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
          	at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)
          	at com.google.gwt.dev.shell.BrowserChannel.reactToMessages(BrowserChannel.java:1668)
          	at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:401)
          	at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:222)
          	at java.lang.Thread.run(Thread.java:619)
          Can someone explain it please?

          Comment


            #6
            Sorry, forgot to mention my environment: I'm using smartwgt 2.2 (just downloaded this version and tried it there), GWT SDK 2.0.3 with Eclipse 3.5 SR1 and jdk 1.6.0 u18 from Windows 7 Enterprise x64 and Mozilla Firefox 3.5.9 as browser.

            Please, advise me something, maybe I'm just doing something wrong... I work with file download in GWT for the first time.

            Thank you in advance.

            Comment

            Working...
            X