Download dynamic file with GWT

前端 未结 3 1117
孤独总比滥情好
孤独总比滥情好 2021-02-14 22:45

I have a GWT page where user enter data (start date, end date, etc.), then this data goes to the server via RPC call. On the server I want to generate Excel report with POI and

相关标签:
3条回答
  • 2021-02-14 23:02

    It's possible to get the binary data you want back through the RPC channel in a number of ways... uuencode, for instance. However, you would still have to get the browser to handle the file as a download.

    And, based on your code, it appears that you are trying to trigger the standard browser mechanism for handling the given mime-type by modifying the response in the server so the browser will recognize it as a download... open a save dialog, for instance. To do that, you need to get the browser to make the request for you and you need the servlet there to handle the request. It can be done with rest urls, but ultimately you will need a serviet to do even that.

    You need, in effect, to set a browser window URL to the URL that sends back the modified response object.

    So this question (about streaming) is not really compatible with the code sample. One or the other (communication protocols or server-modified response object) approach has to be adjusted.

    The easiest one to adjust is the communication method.

    0 讨论(0)
  • 2021-02-14 23:06

    You have to make a regular Servlet, you cannot stream binary data from ReportsServiceImpl. Also, there is no way to call the servlet from ReportsServiceImpl - your client code has to directly invoke the servlet.

    On the client side, you'd have to create a normal anchor link with the parameters passed via the query string. Something like <a href="http://myserver.com/myservlet?parm1=value1&.."</a>.

    On the server side, move your code to a standard Servlet, one that does NOT inherit from RemoteServiceServlet. Read the parameters from the request object, create the excel and send it back to the client. The browser will automatically popup the file download dialog box.

    0 讨论(0)
  • 2021-02-14 23:17

    You can do that just using GWT RPC and Data URIs:

    1. In your example, make your myMethod return the file content.
    2. On the client side, format a Data URI with the file content received.
    3. Use Window.open to open a file save dialog passing the formatted DataURI.

    Take a look at this reference, to understand the Data URI usage:

    Export to csv in jQuery

    0 讨论(0)
提交回复
热议问题