I have something like this in my view:
var url = '@Url.Action("DownloadZip", "Program")' + '?programNums=' + selectedRow; $.ajax({ url: url, dataType: 'json', async: false, success: function (data) { if (data != "Successful") { alert(data); } } });
The controller may return the file or may return a JSON result if an error occurs. Failed to get them to work together.
Here's what it looks like:
public ActionResult DownloadZip(string programNums) { if(string.IsNullOrEmpty(programNums)) { return Json("Error, blank info sent.", JsonRequestBehavior.AllowGet); } var memoryStream = new MemoryStream(); using (var zip = new ZipFile()) { zip.AddFile("C:\\sitemap.txt"); zip.Save(memoryStream); } memoryStream.Seek(0, 0); return File(memoryStream, "application/octet-stream", "archive.zip"); }
I see that a jax value is required to call ajax. Since in my case it returns a file, it cannot work. In any case, to handle what I'm doing, where it can return JSON or a file from an ajax call.
source share