ASP.NET Download all files as Zip

I have a folder on my web server in which there are hundreds of mp3 files. I would like to give the user the opportunity to download the zip archive from each mp3 to a directory from a web page.

I want to compile files programmatically only when necessary. Since the zip file will be quite large, I think I will need to send the zip file to the response stream , since it will be zipped for performance reasons.

Is it possible? How can i do this?

+6
c # zip
source share
4 answers

Here is the code I use to do this with DotNetZip - it works very well. Obviously, you will need to provide variables for outputFileName, folderName and includeSubFolders.

response.ContentType = "application/zip"; response.AddHeader("content-disposition", "attachment; filename=" + outputFileName); using (ZipFile zipfile = new ZipFile()) { zipfile.AddSelectedFiles("*.*", folderName, includeSubFolders); zipfile.Save(response.OutputStream); } 
+7
source share

I can’t believe how easy it was. After reading this , here is the code I used:

 protected void Page_Load(object sender, EventArgs e) { Response.Clear(); Response.BufferOutput = false; Response.ContentType = "application/zip"; Response.AddHeader("content-disposition", "attachment; filename=pauls_chapel_audio.zip"); using (ZipFile zip = new ZipFile()) { zip.CompressionLevel = CompressionLevel.None; zip.AddSelectedFiles("*.mp3", Server.MapPath("~/content/audio/"), "", false); zip.Save(Response.OutputStream); } Response.Close(); } 
+4
source share

You can add a custom handler (.ashx file) that takes the file path, reads the file, compresses it using the compression library, and returns bytes to the end user with the correct content type.

+1
source share
  foreach (GridViewRow gvrow in grdUSPS.Rows) { CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect"); if (chk.Checked) { string fileName = gvrow.Cells[1].Text; string filePath = Server.MapPathfilename); zip.AddFile(filePath, "files"); } } Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=DownloadedFile.zip"); Response.ContentType = "application/zip"; zip.Save(Response.OutputStream); Response.End(); 
0
source share

All Articles