1

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.

My ajax call:

$.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;
      },
});

One attempt of my controller snip.:

[HttpPost, Authorize]
public ActionResult Export(string jsonModel, int? fileType)
{
    MemoryStream workingStream = GenerateExportFile(jsonModel, fileType);
    return File(workingStream, "application/zip", folderName + ".zip");
}

I've also tried using response stream:

[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");
}

Stuff I've looked at: Generating excel then downloading , Download excel via ajax mvc , and Downloading zip through asp mvc

Community
  • 1
  • 1
  • 1
    your dataType says your sending back html but your not, your actually sending back a file. Your then setting your url to the result of this... basically none of this really makes any sense.. – Liam Mar 29 '16 at 16:30
  • your second one your returning a re-direct (again not a zip file) – Liam Mar 29 '16 at 16:31
  • Possible duplicate of [Returning a file to View/Download in ASP.NET MVC](http://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-asp-net-mvc) – Liam Mar 29 '16 at 16:31
  • Ignore the ajax, just open a new window that returns a zip file – Liam Mar 29 '16 at 16:31
  • When you're using traditional AJAX and want a file download, best bet is to add an `iframe` to the page whose `src` points to something that returns your file as an attachment. This will be cross-browser compliant, and would be easy to test and implement. – Joe Enos Mar 29 '16 at 16:51

1 Answers1

0

in javascript (using blockui and jquery-file-download plugins):

        $(window).block();
        $.fileDownload('Export', {
            httpMethod: 'POST',
            data: inputs,
            successCallback: function (url) {
                $(window).unblock();
            },
            failCallback: function (responseHtml, url) {
                $(window).unblock();
                $(window).html(url + '</br>' + responseHtml);
            }
        });

in controller (send file and set cookie):

    public ActionResult Export()
    {
        var e = new ExportZip();
        var request = Request.Unvalidated;
        byte[] data = e.Create(request.Form);

        Response.SetCookie(new HttpCookie("fileDownload", "true") { Path = "/" });
        return File(data, "application/zip", "file.zip");
    }
Alexey Obukhov
  • 834
  • 9
  • 18