Announcement

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

    smartGWT-EE download File

    Hello,
    what is the best way to create a download function.

    my User Workflow:
    1) user select a record in Listgrid.
    2) user clicked the download Button

    3 a) call DMI Method, and return the generated File????? e.g. dsResponse.setDownloadFile????
    3 b) can i use exportData to solve this problem?
    3 c) other suggestions

    I read this post: http://forums.smartclient.com/showthread.php?t=10166&highlight=download but i can't find RPCManager.doCustomResponse() in javaDoc

    NOTE: Content of the generated file is a professional/business XMLDocument. So just a big xml string > 1MB

    I use smartGWT EE!!

    thanks for the help
    TMO

    #2
    doCustomResponse() is in the server JavaDoc (in your SDK, and online here).

    Having called doCustomResponse, work directly with the HttpServletResponse to stream the file.

    When you trigger the request client-side, be sure to set transport to "hiddenFrame". XMLHttpRequest responses will not trigger a Save As dialog (or external application).

    Comment


      #3
      hi Isomorphic,
      thanks for you quick reply.

      Is there a small code example?

      I found this link http://forums.smartclient.com/showthread.php?t=10514 is the code for gwt valid, too?

      Currently, the constructor is defined as following, is the correct? What do you mean exactly with the hidden frame?

      Code:
      public DSResponse getDownloadFile(DSRequest dsRequest, HttpServletRequest servletRequest)  
          throws Exception  
          {......}

      Comment


        #4
        hi,
        please!! give me two small code snippets, for client and server. I can not find the solution! pleeeaaassseee

        thanks in advance
        tmo

        Comment


          #5
          In case someone else comes across this post and wonders what the secret sauce is, this seems to do it. In my case I have a datasource productionOrderDS with a fetch operation binding with operationId="print"

          On the client ...
          Code:
          DSRequest req = new DSRequest();
          req.setTransport(RPCTransport.HIDDENFRAME);
          req.setIgnoreTimeout(true);
          req.setShowPrompt(false);
          req.setOperationId("print");
          productionOrderDS.fetchData(
          		new AdvancedCriteria(ProductionOrder.ID, OperatorId.EQUALS, contextClickedRecord.getAttribute(ProductionOrder.ID)), 
          		null, req);
          On the server ...
          Code:
          private DSResponse printProductionOrders(DSRequest req) throws Exception {
          	DSResponse ordersResp = fetchWithDetails(req);
          	req.rpc.doCustomResponse();
          	HttpServletResponse servletResponse = req.rpc.getContext().response;
          	servletResponse.setContentType("application/pdf");
          	servletResponse.setHeader("Content-Disposition", "inline; filename=\"order.pdf\"");
          	ServletOutputStream os = servletResponse.getOutputStream();
          	for (Map order : (List<Map>) ordersResp.getDataList()) {
          		PdfCreator.getProductionOrderPdf(order, os);
          	}
          	return new DSResponse();
          }
          The server method returns an empty DSResponse object just to satisfy the framework, but that response is ignored by the client. Instead the PdfCreator.getProductionOrderPdf() method writes the PDF content to the servlet output stream and the browser displays the pdf or presents a download dialog. The fetchWithDetails() method just fetches all of the data I need to create the PDF.

          Comment


            #6
            To simplify a bit:

            1. setDownloadResult() is the official way to mark a request as having a download as the response. Then you don't need setTransport(), setIgnoreTimeout() or setShowPrompt() and you can also use downloadToNewWindow if you like

            2. you can just declare the HttpResponse as a parameter to the DMI method instead of looking it up through that series of objects

            3. no need to return an empty DSResponse from the DMI method, just declare the method as returning void.

            Comment


              #7
              Thanks. I expected there was a simpler way.

              Comment


                #8
                When I remove the setIgnoreTimeout() and setShowPrompt(false) and replace it with setDownloadResult() the download works, but the cursor prompt never disappears and all further interaction with the browser is blocked.

                Comment


                  #9
                  You definitely don't need setIgnoreTimeout(). If you've got other settings that enable the prompt (like maybe you've set DataSource.showPrompt or you've got a call to showPrompt in transformRequest) that might explain needing to disable showPrompt on the specific request.

                  Comment


                    #10
                    I have the same problem as well. The file is downloaded correctly, but the progress stays and all interaction is locked. Need to refresh the page to restore interaction.

                    No calls DataSource.showPrompt or any other explicit enabling of prompt.

                    Client:
                    Code:
                    this.getView().getLogFileRecord().addRecordClickHandler(new RecordClickHandler() {
                    			
                    			@Override
                    			public void onRecordClick(RecordClickEvent event) {
                    				// Open log file in a new window based on the transmission id clicked 
                    				Integer transmitSesssionId = 6;
                    				
                    				DSRequest req = new DSRequest();
                    				req.setDownloadResult(true);
                    				req.setDownloadToNewWindow(true);
                    				//req.setTransport(RPCTransport.HIDDENFRAME);
                    				//req.setIgnoreTimeout(true);
                    				//req.setShowPrompt(false);
                    				TransmitSessionsPagePresenter.this.logFileDS.fetchData(new Criteria("transmitSessionId", transmitSesssionId.toString()), null, req);
                    			}
                    		});
                    Server:
                    Code:
                    public class LogFileDMI {
                    	
                    	public void fetch(DSRequest req, RPCManager manager, HttpServletResponse servletResponse) throws Exception {
                    		manager.doCustomResponse();
                    		
                    		String transmitSessionId = req.getCriteria().get("transmitSessionId").toString();
                    		String fileName = "Transmission_" + transmitSessionId + "_Log.txt";
                    		
                    		servletResponse.setContentType("text/plain");
                    		servletResponse.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
                    		ServletOutputStream os = servletResponse.getOutputStream();
                    		
                    		// Write log file to the output stream
                    		os.write(transmitSessionId.getBytes("UTF8"));
                    		os.flush();
                    		os.close();
                    	}
                    }
                    DataSource:
                    Code:
                    <DataSource
                    	ID="logFile"
                        serverType="generic"
                    >	
                    
                    	<fields>
                    		<field name="transmitSessionId" type="integer" />
                    	</fields>
                    
                    	<operationBindings>
                            <binding operationType="fetch" serverMethod="fetch">
                            	<serverObject  lookupStyle="new" className="com.amgreetings.telxondatawedge.callcenter.server.LogFileDMI"/>
                            </binding>
                        </operationBindings>
                       
                    </DataSource>

                    Comment


                      #11
                      Just for completeness - like the other poster you may need to setShowPromt(false). We have identified and fixed one case where a DataSource-level setting for showPrompt could prevent the prompt from being automatically disable for downloads, so this won't be necessary in the future regardless.

                      Comment

                      Working...
                      X