我的MVC视图中有jqgrid,它显示可用文件的数量。在选择jqGrid的每一行中的复选框后,用户可以以zip格式下载单个文件或多个文件。单个文件下载工作正常,并能够提示用户保存文件但是当我以zip格式下载文件时,我没有得到任何保存提示将文件保存在客户端计算机上。 Zip文件在文件夹中正常创建,但没有提示保存。我不知道我在哪里做错了。请参阅以下代码并在此问题上帮助我....
**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()
{
}