title: Exporting in MVC with ajax
body: <p>I'm trying to export a zip file created in my controller through ajax. I've looked through several related questions, but they're all slightly different, and I haven't been able to connect the dots. Sometimes, I get nothing returned. Other times, my ajax goes to the window location and everything breaks.</p>
  
  <p>My ajax call: </p>
  
  <pre><code>$.ajax({
       url: "/Somewhere/Export",
       type: "POST",
       dataType: 'html',
       data: {
           jsonModel: JSON.stringify(modelData),
           fileType: $("#toggle-2").val()
        },
        success: function (returnValue) {
             window.location = '/Somewhere/Export/Download?file=' + returnValue;
        },
  });
  </code></pre>
  
  <p>One attempt of my controller snip.:</p>
  
  <pre><code>[HttpPost, Authorize]
  public ActionResult Export(string jsonModel, int? fileType)
  {
      MemoryStream workingStream = GenerateExportFile(jsonModel, fileType);
      return File(workingStream, "application/zip", folderName + ".zip");
  }
  </code></pre>
  
  <p>I've also tried using response stream:</p>
  
  <pre><code>[HttpPost, Authorize]
  public ActionResult Export(string jsonModel, int? fileType)
  {
      MemoryStream workingStream = GenerateExportFile(jsonModel, fileType);
      Response.Clear();
      Response.AddHeader("Content-Disposition", "attachment; filename=" + folderName + ".zip");
      Response.ContentType = System.Net.Mime.MediaTypeNames.Application.Zip;
      workingStream.WriteTo(Response.OutputStream);
      Response.End();
      return RedirectToAction("Index");
  }
  </code></pre>
  
  <p>Stuff I've looked at:
  <a href="https://stackoverflow.com/questions/17548796/generating-an-excel-file-then-downloading-it-from-browser-in-asp-net-mvc-applic">Generating excel then downloading</a>
  ,
  <a href="https://stackoverflow.com/questions/16670209/download-excel-file-via-ajax-mvc">Download excel via ajax mvc</a>
  , and 
  <a href="https://stackoverflow.com/questions/29907291/downloading-of-zip-file-through-asp-net-mvc-using-dotnetzip">Downloading zip through asp mvc</a></p>