开发者

downloaded zip file corrupted

开发者 https://www.devze.com 2023-03-21 07:19 出处:网络
I\'m using following code to download a zip file protected void btnDownloadNow_Click(object sender, EventArgs e)

I'm using following code to download a zip file

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

        }

and i have the following javascript on the same page

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

            }
        }

my issue is the downloaded zip file is corrupted due to above javascript code.when i remove the javascript from the page zip file works fine.why th开发者_如何学Gois javascript cause such an error


That's ASP.NET code, not javascript.

I can't see all the necessary code but...

Your HTTP Response is getting all mangled up. Clear your HTTPResponseHeader before you send your file, flush afterwards, and make sure you end the HTTPContext Response

Something like this:

    //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

精彩评论

暂无评论...
验证码 换一张
取 消