下载的zip文件已损坏

时间:2011-07-18 17:34:33

标签: asp.net zip download

我正在使用以下代码下载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);

        }

我在同一页面上有以下javascript

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;
                    }
                }

            }
        }

我的问题是由于上面的javascript代码已下载的zip文件已损坏。当我从页面zip文件中删除javascript工作正常。为此javascript导致此类错误

1 个答案:

答案 0 :(得分:4)

这是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();