title: Call controller in Ajax to download a file in Grails
body: <p>I'm new to Grails and I'm having troubles for downloading a document generated in my controller.</p>
  
  <p>My jQuery</p>
  
  <pre><code>$("#exportAllSelectedData").click(function() {
     var dataToExport = $("#dataToExport").val();
     jQuery.ajax(
         {type:'POST',
          data:'dataToExport=' + dataToExport ,
          dataType: 'json',
          url:'/myapp/mycontroller/exportPreferences'});;
  });
  </code></pre>
  
  <p>My Controller</p>
  
  <pre><code>def exportPreferences ()
  {
      File file = File.createTempFile("export",".xml");
      String dataToWrite = params.dataToExport;
      file.write(dataToWrite);
      response.contentType = "application/octet-stream";
      response.setHeader "Content-disposition", "attachment; filename=${file.name}";
      response.outputStream &lt;&lt; file.bytes;
      response.outputStream.flush();
  }
  </code></pre>
  
  <p>I was expecting to download the outputStream with my browser but nothing happened.
  What am I doing wrong ?</p>
  
  <p>Edit :
  Thanks Rahul.
  It worked fine with:</p>
  
  <pre><code>$("#exportAllSelectedData").click(function() {
    var dataToExport = $("#dataToExport").val();
    window.location="&lt;g:createLink controller="mycontroller" 
    action="exportPreferences"/&gt;"+"?dataToExport="+dataToExport
  });
  </code></pre>