Monday, 26 April 2010

Using SharpZibLib over HTTPS SSL

I've been using SharpZipLib to zip up content and stream it to the user for some time now, and it's proved very reliable, robust and easy to use.

I did however recently have a problem that turned out to be IE-specific, and only when streaming to an SSL encrypted stream. Instead of a downloaded file, users would see the this error.

"Internet Explorer cannot download download from xxxxxxxxx
Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later."


The solution turned out to be simple. Prior to setting other properties (such as AddHeader) you need to clear the Response object like so:

Response.Clear()
Response.ClearHeaders()

And so now my zip and download routine looks like this:
''' 
    ''' zips up any file you send it and initiates a download
    ''' 
        Public Sub ZipAndDownload(ByVal strPath As String, ByVal strFileName As String)

        Response.Clear()
        Response.ClearHeaders()
        Response.ContentType = "application/zip"
        Response.AddHeader("Content-Disposition", "inline; filename=" & strFileName & ".zip")

        ' FastZip - zip all the file and folders
        ' and stream it through the Response OutputStream
        Dim fz As New ICSharpCode.SharpZipLib.Zip.FastZip()
        fz.CreateZip(HttpContext.Current.Response.OutputStream, strPath, True, Nothing, Nothing)

    End Sub

No comments:

Post a Comment

Comments are moderated, so you'll have to wait a little bit before they appear!