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:


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:

      url: Alfresco.constants.PROXY_URI + "export/data/post",
      method: Alfresco.util.Ajax.POST,
                  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){
      failureMessage: "Failed to retrieved the Hierarchy!",
      failureCallback: {
          fn: function(response){},
      execScripts: true



The webscript's descriptor is this:

  <shortname>Export to Excel Webscript</shortname>
  <description>Sample Export to excel webscript</description>
  <format default="xls">argument</format>
  <transaction allow="readonly">required</transaction>


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";
      Log log = LogFactory.getLog(ExportDataExcel.class);

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

      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;
          throw new WebScriptException(Status.STATUS_BAD_REQUEST, "JSON errato " + jsonO);
               //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 =;
                    if(key.equals("site")) nomeSito = datiExcel.getString(key);
                    if(key.equals("fileName")) nomeFile = datiExcel.getString(key);
                         // 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