0
我在我的MVC視圖中顯示了jqgrid,它顯示了可用文件的數量。用戶可以在jqGrid的每一行中選擇複選框後下載單個文件或多個zip格式的文件。單個文件下載工作正常,能夠提示用戶保存文件,但是當我以zip格式下載文件時,我沒有得到任何保存提示以將文件保存在客戶機上。 Zip文件在文件夾中創建正常,但不提示保存。我不知道我在做什麼錯。請看下面的代碼,並幫助我在這個問題上....無法提示保存對話框以下載MVC中的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()
{
}
如果你看到我的代碼,我有評論從下第三行。我試圖使用文件,但沒有在我的情況下工作,所以我嘗試了不同的方式。文件工作正常,當我下載單個罰款,但是當我下載zip文件,然後它不工作.... – user853320 2012-03-22 21:24:35
$(「#btnDownloadZip」)。click(function(){ var files = $(「#list」) .jqGrid( 'getGridParam', 'selarrrow')的toString(); 如果(文件== ''){ 警報( '請選擇要下載的文件...'); 回報; } VAR的fileList = files.split( 「」); $ .POST( '<%= Url.Action( 「DownloadZip」)%>',{filesToZip:的fileList},handleSuccess); // $ .getJSON(「/ Home/DownloadZip /」,{filesT oZip:fileList}); }); – user853320 2012-03-23 00:00:52
嗨,我解決了它。我用window.location =「/ Home/DownloadZip?filesToZip =」+ fileList;而不是Url.Action或getJSON。謝謝.... – user853320 2012-03-23 00:43:34