title: Download server generated CSV with ASP .NET MVC and jQuery AJAX request
body: <p>I am working on mechanism to export data in <strong>CSV</strong> format. 
  I am sending data in <code>JSON</code> format using <strong>jQuery</strong> :</p>
  
  <pre><code>var data = JSON.stringify(dataToSend);
   $.post('DumpToCSV', { 'data': data });
  </code></pre>
  
  <p>Then in the controller I am generating a <strong>CSV</strong> file :</p>
  
  <pre><code> public ActionResult  DumpToCSV(string data)
      {
          Response.Clear();
  
          XmlNode xml = JsonConvert.DeserializeXmlNode("{records:{record:" + data + "}}");
  
          XmlDocument xmldoc = new XmlDocument();
          //Create XmlDoc Object
          xmldoc.LoadXml(xml.InnerXml);
          //Create XML Steam 
          var xmlReader = new XmlNodeReader(xmldoc);
          DataSet dataSet = new DataSet();
          //Load Dataset with Xml
          dataSet.ReadXml(xmlReader);
          //return single table inside of dataset
          var csv = CustomReportBusinessModel.ToCSV(dataSet.Tables[0], ",");
  
          HttpContext context = System.Web.HttpContext.Current;
          context.Response.Write(csv);
          context.Response.ContentType = "text/csv";
          context.Response.AddHeader("Content-Disposition", "attachment;filename=Custom Report.csv");
          Response.End();
          return null;
      }
  </code></pre>
  
  <p>It returns back the <strong>CSV</strong> in response, but how do I tell the browser to donwload it?</p>
  
  <blockquote>
    <p>The difference between this topic :
     <a href="https://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-mvc">Returning a file to View/Download in ASP.NET MVC</a>
    Is that I'm using AJAX request</p>
  </blockquote>