简体   繁体   中英

How to download folder containing files as zip from server using asp.net C#

I want to download a folder with its files in various mime type. My virtual path is " http://localhost/attachments/ ". My sub folders are "certificates/id". So when clicking on a grid i pass id to the download page. But it throws an exception like virtual path is invalid. ' http://localhost/attachments/certificates/id )'.

In below code, Request.Params[0] meant id, this points the endlevel folder which i want to make a zip folder.

Any guidance would be grateful.

            using (ZipFile zip = new ZipFile())
            {
                string VirtualPath = ConfigurationManager.AppSettings.Get("AttachmentsShowVirtualPath");
                string Path = string.Empty;
                Path = "certificates" + "/";

                string folderPath = VirtualPath + Path + Request.Params[0] + "/";

                zip.CompressionLevel = CompressionLevel.None;

                zip.AddSelectedFiles(".", Server.MapPath(folderPath), "", false);
                zip.Save(Response.OutputStream);
            }

First, are you using DotNetZip? I have never use it, but try changing the . to *.*

If not getting the file, try to do Directory.GetFiles to check if eveything is pointing correctly and your code has read permission over the directory https://msdn.microsoft.com/en-us/library/07wt70x2%28v=vs.110%29.aspx

Or try to use official Zip class from https://msdn.microsoft.com/en-us/library/system.io.compression.zipfile%28v=vs.110%29.aspx With this method: ZipFile.CreateFromDirectory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM