sim.plified.com

Chris Pollock

Chris Pollock - web developer (PHP/mySQL & ASP.NET)
undivided… my thoughts on world, family, church, business, technology and Jesus Christ (all in all)

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.

No Comments, Comment or Ping

Reply to “Package Files for Zip Download with ASP.net”

My Pictures

CIMG6647CIMG6646CIMG6643CIMG6641CIMG6640CIMG6638CIMG6637CIMG6635CIMG6632CIMG6629

Chris Pollock

Web Developer - proficient in both PHP and ASP.NET.
Rochester, New York

View my web developement site.

View Chris Pollock's LinkedIn profile

My Pictures

CIMG6647CIMG6646CIMG6643CIMG6641CIMG6640CIMG6638CIMG6637CIMG6635CIMG6632CIMG6629