Package Files for Zip Download with ASP.net
In some of my other posts I've talked about an application I have built that accesses the Flickr API and provides campus administrators with a filtered view of the images in our Flickr account.
One of the features of this site is a photo bucket where users can "mark as favorite" certain images. I have now included a function that allows them to download all of their favorite photos in a zip file.
Dependencies:
This app utilizes the SharpZip Library – available here.
Props:
This tutorial was particularly helpful for me – click here. Although my implementation is quite different.
Basically the user clicks on a button and the zip file presents itself for download
Markup:
<asp:LinkButton ID="lbDownload" runat="server" OnClientClick="$(this).html('Creating Zip File... this may take a moment')">Download All Images (.zip)</asp:LinkButton>
Note that I am using jQuery to handle some client feedback.
Code Behind:
Public Function CreateZipOfFavorites(ByVal intUserId As Integer) As String Try 'Get list of users favoriates from the database Dim dsFavs As DataSet = UserFavorites(intUserId) If Not IsNothing(dsFavs) Then 'Zip filename will be a simple datetime string Dim strNow As String = String.Format("{0:MMM-dd-yyyy_hh-mm-ss}", System.DateTime.Now) 'Create the Zip library Dim myZipStream As New ZipOutputStream(File.Create(HttpContext.Current.Server.MapPath("./Zips/") + strNow + ".zip")) 'This is the Photo object that comes from the FlickrNet.dll Dim myPhoto As PhotoInfo 'Loop through the favorites and add them to zip For Each drFav As DataRow In dsFavs.Tables(0).Rows 'Grab the files from flickr myPhoto = myFlickr.PhotosGetInfo(ImageGetFlickrId(drFav("pc_id"))) 'Sycronously grab the data (note, large files will take time) Dim fileClient As New Net.WebClient() Dim dataBuffer As Byte() = fileClient.DownloadData(myPhoto.OriginalUrl) 'Put the images into the zip library Dim zipEntry As New ZipEntry(myPhoto.PhotoId & "." & myPhoto.OriginalFormat) myZipStream.PutNextEntry(zipEntry) myZipStream.Write(dataBuffer, 0, dataBuffer.Length) Next 'Clean up and close up myZipStream.Finish() myZipStream.Close() 'Return the file name Return strNow & ".zip" Else 'Return blank if no zip is created Return "" End If Catch ex As Exception myErrorHandler.Exception = ex myErrorHandler.DefaultHandler() Return "" End Try End Function
Protected Sub lbDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbDownload.Click Dim fileZip As String = CreateZipOfFavorites(myPhotoCentral.UserGetId(Session("LogonUser"))) If fileZip <> "" Then Session("FlashMessage") = "Zip file created." Response.ContentType = "application/force-download" Response.AppendHeader("Content-disposition", "attachement; filename=" & fileZip) Response.TransmitFile("Zips/" & fileZip) Else Session("FlashMessage") = "Zip file creation failed." End If End Sub
That's about it for the implementation. It's pretty straight forward. Let me know if you have comments or questions.















3 Comments, Comment or Ping
Vijay
Hi
I am not using the Photonfo object to download files from the WebClient
I wants to zip files available from on Project/Website…
How can I implement that… please help me
waiting for your reply….
Thanks
Vijay
Nov 7th, 2008
Gautam
Chris,
Your little pc of code was really useful in setting up my download function. I have been looking at the SharpZip for a while, yours is the most straight forward implementation indeed.
Cheers,
Gautam
Aug 2nd, 2010
alex
hi, I am trying to download files from my website, i have used icsharp utitility to zip the file on the server and then download. it works fine but when the selected files sizes grows let say 100 MB then system is hanged and the browser consider that a script is running and its causing the page to be hanged, so the user browser is hanged and he needs to restart the page again.
i am using:
c#, asp.net, XML, Ajax.
what i need to add so that it avoid the hang on proces ?
Jan 14th, 2011
Reply to “Package Files for Zip Download with ASP.net”