AnsweredAssumed Answered

Export data-list in excel (running a post ajax request): how retrieve the file?

Question asked by zlucy82 on Feb 27, 2018
Latest reply on Mar 2, 2018 by zlucy82

Hi all,

 

i have implemented a function that exports site data-list informations in a excel file, but i have a problem to retrieve the file.

 

To do this, i've followed the tutorial of KRUTIK JAYSWAL: http://www.krutikjayswal.com/2017/07/alfresco-webscript-export-data-in-excel.html.

 

The difference with its code is that i don't run a redirect to call the webscript (window.location.href = Alfresco.constants.PROXY_URI+"export/excel/?nodeRef="+file.nodeRef;) but i need send a json object to the java controller of the webscript.

 

I've performed an ajax post request to send informations through a webscript that extends the AbstractWebScript webscript type. The java controller class processes the json object and writes the response ( file excel) in the WebScriptResponse object (incomplete function).

 

Now, i dont't know how get back the file because the webscript returns an incomprensible response.

 

The javascript function that runs the post request is this:

Alfresco.util.Ajax.request({
      url: Alfresco.constants.PROXY_URI + "export/data/post",
      method: Alfresco.util.Ajax.POST,
      dataObj:
                {
                  site: this.options.siteId,
                  fileName: "report-" + today.getFullYear() + '-' + (today.getMonth()+1) + '-' + today.getDate()
                  + '-' + today.getHours() + '-' + today.getMinutes() + '-' + today.getSeconds(),
                  rows: req                 
                },
      requestContentType: Alfresco.util.Ajax.JSON,
      successMessage: "Successfully retrieved the Hierarchy.",
      successCallback: {
          fn: function(response){
                alert(response.serverResponse.responseText);
               },
          scope:this
      },
      failureMessage: "Failed to retrieved the Hierarchy!",
      failureCallback: {
          fn: function(response){},
          scope:this
      },
      execScripts: true
});

 

 

The webscript's descriptor is this:

<webscript>
  <shortname>Export to Excel Webscript</shortname>
  <description>Sample Export to excel webscript</description>
  <url>/export/data/post</url>
  <format default="xls">argument</format>
  <authentication>user</authentication>
  <transaction allow="readonly">required</transaction>
</webscript>

 

The java class of the webscript's controller is this:

/**
* A java class controller of export data in excel
*
*/

public class ExportDataExcel extends AbstractWebScript {
     private String fileName = "ExcelSheet";
     private String format = "xlsx";
    
     //logging
      Log log = LogFactory.getLog(ExportDataExcel.class);

      /**
       * This method is inherited from AbstractWebscript class and called
       * internally.
       */

      @Override
      public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException{
     
           String nomeSito = "";
           String nomeFile = "";
           
           
           //Controlliamo che la richiesta non sia vuota
           if (req.getContent() == null)
          throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Corpo del POST assente.");           
         
         JSONObject datiExcel = null;
          
          //check json object
          Object jsonO = req.parseContent();
          if (jsonO instanceof JSONObject && jsonO != null)
               datiExcel = (JSONObject)jsonO;
          else
          throw new WebScriptException(Status.STATUS_BAD_REQUEST, "JSON errato " + jsonO);
       
       
       
           try{
               //si dovrebbe controllare che sia un JSON valido
               Map<String, Object> map = new HashMap<String, Object>();
               Iterator<String> keyItr = datiExcel.keys();
               while(keyItr.hasNext()) {
                    String key = keyItr.next();
                    log.debug(key);
                    if(key.equals("site")) nomeSito = datiExcel.getString(key);
                    if(key.equals("fileName")) nomeFile = datiExcel.getString(key);
                    if(key.equals("rows")){
                         // TO DO ...
                    
                    }
                   map.put(key, datiExcel.getString(key));
               }
               
          }
          catch (JSONException e) {
              throw new RuntimeException(e);
          }
          
          
          //TO DO: pass the json object information to process them
          writeDataInExcel(res);
           
      }
      .....

Outcomes