2012-03-22 27 views
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() 
    { 
    } 

回答

0

Asp.Net MVC有一个函数名文件您可以使用从控制器动作返回文件。

this问题,显示了如何恢复一个文件

+0

如果你看到我的代码,我有评论从下第三行。我试图使用文件,但没有在我的情况下工作,所以我尝试了不同的方式。文件工作正常,当我下载单个罚款,但是当我下载zip文件,然后它不工作.... – user853320 2012-03-22 21:24:35

+0

$(“#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

+0

嗨,我解决了它。我用window.location =“/ Home/DownloadZip?filesToZip =”+ fileList;而不是Url.Action或getJSON。谢谢.... – user853320 2012-03-23 00:43:34