I have jqgrid in my MVC view, which displays the number of files available. User can download the single file or multiple files in zip format after selecting the checkbox in each row of jqGrid. Single file download is working fine and able to prompt to user to save the file but when I download the files in zip format, i don't get any save prompt to save the file on client machine. Zip file is creating fine in the folder but not prompting to save. I don't know where I am doing wrong. Please see the following code and help me on this issue....
**Controller**
[HttpGet]
public ActionResult DownloadZip(String[] filesToZip)
{
//checking if there any file to zip
if (filesToZip == null || filesToZip.Length < 1 || filesToZip.Count() == 0)
return (new EmptyResult());
//creating dynamic zip file name
var downloadFileName = string.Format("Test_{0}.zip", DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss"));
//zipping the files
using (ZipOutputStream zipos = new ZipOutputStream(System.IO.File.Create(Path.Combine(Server.MapPath(uploadLocation), downloadFileName))))
{
// 0-9, 9 being the highest compression
zipos.SetLevel(9);
//temp buffer to hold 4gb data max
byte[] buffer = new byte[4096];
foreach (string file in filesToZip)
{
ZipEntry newEntry = new ZipEntry(Path.GetFileName(file));
zipos.PutNextEntry(newEntry);
using (FileStream fs = System.IO.File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
zipos.Write(buffer, 0, sourceBytes);
}
while (sourceBytes > 0);
}
}//end files loop
}
System.IO.FileInfo zipFile = new System.IO.FileInfo(Path.Combine(Server.MapPath(uploadLocation), downloadFileName));
// clear the current output content from the buffer
Response.Clear();
// add the header that specifies the default filename for the
// Download/SaveAs dialog
Response.AddHeader("Content-Disposition", "attachment; filename=" + downloadFileName);
// add the header that specifies the file size, so that the browser
// can show the download progress
Response.AddHeader("Content-Length", zipFile.Length.ToString());
// specify that the response is a stream that cannot be read by the
// client and must be downloaded
Response.ContentType = "application/zip";
// send the file stream to the client
Response.WriteFile(zipFile.FullName);
//ControllerContext.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + downloadFileName);
//return File(Path.Combine(Server.MapPath(uploadLocation), downloadFileName), "application/zip", downloadFileName);
return (new EmptyResult());
}
**View**
$("#btnDownloadZip").click(function () {
var files = $("#list").jqGrid('getGridParam', 'selarrrow').toString();
if (files == '') {
alert('Please select a file to download...');
return;
}
var fileList = files.split(",");
$.post('<%= Url.Action("DownloadZip") %>', { filesToZip: fileList }, handleSuccess);
//$.getJSON("/Home/DownloadZip/", { filesToZip: fileList });
});
function handleSuccess()
{
}