Announcement

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

    How to save an attachment downloaded using RPCManager.sendRequest ()

    I need to export RestDataSource data as an XLS attachment.

    Please note we are just evaluating to buy SmartGWT Pro or better license for Excel export and other useful features (and we are also aware of "client-driven export") but not for the current project.
    I'm using SmartClient Version: SC_SNAPSHOT-2011-12-05/LGPL Development Only (built 2011-12-05)

    So I actually need to export a file based on the same data that the datasource serves to the ListGrid instance. The request should simply bring a different mime type param and additional metadata just to switch behavior: it usually produces JSON, this time it should produce an attachment.

    Since my datasource does some parameters elaboration (overriding transformRequest ()) and the server is aware of that parameters structure, I've managed to forge the request reusing my DataSource logic and sending it via RPCManager.sendRequest (). It sends correct data to the server, and the server responds with the attachment, but the client don't show a save dialog.
    This is the relevant code
    Code:
    final DSRequest dsr = new DSRequest();
    
    dsr.setActionURL(actionURL);
    dsr.setDownloadResult(true);
    dsr.setDownloadToNewWindow(true);
    dsr.setOperationType(DSOperationType.ADD);
    dsr.setHttpMethod("POST");
    dsr.setUseSimpleHttp(true);
    dsr.setContentType("application/json");
    dsr.setTransport(RPCTransport.XMLHTTPREQUEST);
    dsr.setExportResults(true);
    final Map<String, String> httpHeaders = new HashMap<String, String>();
    httpHeaders.put("Accept", "application/vnd.ms-excel");
    dsr.setHttpHeaders(httpHeaders);
    ...
    dsr.setData(payload);
    
    RPCManager.sendRequest(dsr);
    What's wrong with my code?
    Is there a better to achieve this behavior or a similar one (that re-uses the original datasource request preparation logic)?
    Last edited by d.cavestro; 17 Jan 2013, 04:04. Reason: Clarified title

    #2
    Could you elaborate on what you're doing here - are you targeting a built-in servlet of the SmartGWT server? If so, which one?

    Are you hoping that the SmartGWT server will generate the XLS or are you doing that yourself.

    Note that your request doesn't really make sense because XmlHttpRequests cannot download files.

    Also, if you are targeting your own server-side code, the HTTP header "Content-Disposition:attachment" is how you advertise to the browser that you mean to have a file downloaded.

    Comment


      #3
      you are right

      Originally posted by Isomorphic View Post
      Note that your request doesn't really make sense because XmlHttpRequests cannot download files.
      Sorry, you are right (as usual).
      I wasn't really aware that XmlHttpRequest doesn't support at all downloading attachments (for other folks unaware like me, XmlHttpRequest is the core component used for asynchronous requests by ajax-enabled libraries like SmartClient/GWT, jQuery and so on). SmartGWT can't work magic... or at least, not yet :-)

      However I'm using my REST service at server side, and it's sending the HTTP header "Content-Disposition:attachment" in the response.
      So I'm going to fall back on doing a plain HTML form POST through some JSNI (that's not the only way). BTW it sends a request without the Content-Type and Accept headers that I'd have sent with an asyncronous call, cause as far as I know there's no way to control request headers through a plain html form request. Hence I have to modify the service in order to intercept the request without the relevant headers.

      Comment


        #4
        That sounds like it will work for your custom servlet.

        It's unclear if you're using Pro or better, but there's a sample of how to do a Custom Download in the Pro/EE Showcase.

        Comment

        Working...
        X