Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to download multiple files as ZIP in asp.net

the code is following

C#
var downloadFileName = string.Format(projectno, DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss"));
                Response.ContentType = "application/zip";
                Response.AddHeader("Content-Disposition", "filename=" + downloadFileName );
                using (var zip = new ZipFile())
                {
                    var readMeMessage = string.Format("Your ZIP file {0} contains the following files:{1}{1}", downloadFileName, Environment.NewLine);
                    foreach (GridViewRow gvr in grdUploads.Rows)
                    if (((CheckBox)gvr.Cells[6].FindControl("dwnldCheck")).Checked == true)
                        {
                            string filename = Server.MapPath("../" + ((Label)gvr.Cells[3].FindControl("lblTitle")).Text);
                            readMeMessage += string.Concat("\t* ", filename, Environment.NewLine);
                            zip.AddFile(filename, projectno);

                        }
                    zip.AddEntry("README.txt", readMeMessage, Encoding.ASCII);
                    zip.Save(Response.OutputStream);
                }



everything is working fine,but its saving as unknown format,not as rar file




please help
Posted

You need to add extension in your code after downloadFileName at 3rd line.

C#
var downloadFileName = string.Format(projectno, DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss"));
        Response.ContentType = "application/zip";
        Response.AddHeader("Content-Disposition", "filename=" + downloadFileName + ".zip");
        using (var zip = new ZipFile())
        {
            var readMeMessage = string.Format("Your ZIP file {0} contains the following files:{1}{1}", downloadFileName, Environment.NewLine);
            foreach (GridViewRow gvr in grdUploads.Rows)
                if (((CheckBox)gvr.Cells[6].FindControl("dwnldCheck")).Checked == true)
                {
                    string filename = Server.MapPath("../" + ((Label)gvr.Cells[3].FindControl("lblTitle")).Text);
                    readMeMessage += string.Concat("\t* ", filename, Environment.NewLine);
                    zip.AddFile(filename, projectno);

                }
            zip.AddEntry("README.txt", readMeMessage, Encoding.ASCII);
            zip.Save(Response.OutputStream);
        }
 
Share this answer
 
Comments
kishore Rajendran 18-May-11 5:24am    
thats already i tried,but that is not the actual error...thanks
That's Aragon 18-May-11 6:42am    
I am agree with Wayne Gaylard's response. Could you please provide more information regarding the issue. May be it will provide more depth to the issue.
You can do it as mentioned below
<pre lang="c#">ZipFile multipleFilesAsZipFile = new ZipFile();
        Response.AddHeader("Content-Disposition", "attachment; filename=DownloadedZipFile.zip");
        Response.ContentType = "application/zip";
        foreach (ListItem fileName in checkBoxList.Items)
        {
            if (fileName.Selected)
            {
                string filePath = Server.MapPath("~/www.CsharpAspNetArticles.com/" + fileName.Value);
                multipleFilesAsZipFile.AddFile(filePath,string.Empty);
            }
        }
        multipleFilesAsZipFile.Save(Response.OutputStream);


Refer Download Multiple Files As Zip File In Asp.Net for more info
 
Share this answer
 
I fail to see where you are adding the extension .zip to the downloadFileName . One thing I noticed is that you use a lot of var keyword, instead of declaring strong type variables. As far as I am concerned his is bad practice, and should really be avoided, if downloadFileName is a string declare it as such.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900