2011-07-18 226 views
0

我用下面的代碼來下載一個zip文件下載的zip文件損壞

protected void btnDownloadNow_Click(object sender, EventArgs e) 
    { if (cblFiles.SelectedItem == null) 
     { 

      RegisterStartupScript("as","You must select one or more files to download."); 

     } 


     var downloadFileName = string.Format("Transmittal.zip", DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss")); 
     Response.ContentType = "application/zip"; 
     Response.AddHeader("Content-Disposition", "filename=" + downloadFileName); 


     using (var zip = new ZipFile()) 
     { 

      if (!string.IsNullOrEmpty(txtZIPPassword.Text)) 
      { 
       zip.Password = txtZIPPassword.Text; 


       zip.Encryption = EncryptionAlgorithm.WinZipAes128; 
      } 


      var readMeMessage = string.Format("Your ZIP file {0} contains the following files:{1}{1}", downloadFileName, Environment.NewLine); 


      foreach (ListItem li in cblFiles.Items) 
       if (li.Selected) 
       { 

        readMeMessage += string.Concat("\t* ", li.Text, Environment.NewLine); 

        zip.AddFile(li.Value, "Your Files"); 
       } 



      zip.AddEntry("README.txt", readMeMessage, Encoding.ASCII); 


      zip.Save(Response.OutputStream); 

     } 

和我有同樣的頁面

function CheckAllPDF(value) { 
      var elementRef = document.getElementById("<%= cblFiles.ClientID %>"); 
      var checkBoxArray = elementRef.getElementsByTagName('input'); 

      var checkBoxLabelArray = elementRef.getElementsByTagName('label'); 

      var checkedValues = ''; 

      for (var i = 0; i < checkBoxArray.length; i++) { 

       var checkBoxRef = checkBoxArray[i]; 
       var checkBoxLabelRef = checkBoxLabelArray[i]; 
       var stringt = checkBoxLabelRef.innerHTML; 
       var match = stringt.indexOf(".pdf"); 
       if (value == true) { 
        document.getElementById("<%=Checkbox1.ClientID %>").checked = false; 
        document.getElementById("<%=cc.ClientID %>").checked = false; 
        if (match != -1) { 
         checkBoxRef.checked = true; 

        } 
        else { 
         checkBoxRef.checked = false; 
        } 
       } 
       else { 
        // checkBoxRef.checked = false; 
        if (match != -1) { 

         checkBoxRef.checked = false; 
         document.getElementById("<%=Checkbox1.ClientID %>").checked = false; 
        } 
        else { 
         // checkBoxRef.checked = true; 
        } 
       } 

      } 
     } 

我的問題是下載的zip文件是在下面的JavaScript由於上面的JavaScript代碼損壞。當我從頁面中刪除JavaScript壓縮文件工作正常。爲什麼這個JavaScript導致這樣的錯誤

回答

3

這是ASP.NET代碼,而不是JavaScript。

我無法看到所有必要的代碼,但...

你的HTTP響應越來越全部錯位了。清除HTTPResponseHeader您發送文件之前,沖洗之後,並確保你最終的HttpContext響應

事情是這樣的:

//clear your headers 
    Response.Clear(); 

    var downloadFileName = string.Format("Transmittal.zip", DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss")); 
    Response.ContentType = "application/zip"; 
    Response.AddHeader("Content-Disposition", "filename=" + downloadFileName); 

    //Write (Send) your file 
    Response.Write( ...); 
    Response.Flush(); 
    HttpContext.Current.Response.End(); 
+0

感謝名單及其工作現在! – chamara