locked
Zip file download through handler fails in IE RRS feed

  • Question

  • User1315518678 posted

    Hello,

    I've been slaving over this for a good few hours to no avail, so any help would be greatly appreciated. I have a load of XML files stored on my server, that I'm putting in to a Zip file allowing my users to download the Zip file through a handler.

    It all works fine, apart from in Internet Explorer, where it fails on the first occasion and says the file can't be downloaded, then works when you press Retry. From much Googling around, it would seem to be something to do with the Expires, but this is set to 2 minutes which is by far more than the time the download takes (around 2-3 seconds at highest). I've tried messing round with the cachability with no luck.

    Any advice very very gratefully received.

    Thanks

    Mark


    Here's my code:

        Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    
    		Dim sListOfPaths As Generic.List(Of String) = CType(HttpContext.Current.Session("XMLLogsListOfPaths"), Generic.List(Of String))
    
    		Me.DownloadZipToBrowser(context, sListOfPaths)
    
    	End Sub
    
    
    	Private Sub DownloadZipToBrowser(ByVal context As HttpContext, sFileList As Generic.List(Of String))
    
    		'set up the file to be downloaded
    
    		context.Response.Clear()
    		context.Response.Expires = 2
    		context.Response.ExpiresAbsolute = Date.Now
    		context.Response.BufferOutput = False
    		context.Response.ContentType = "application/zip"
    		context.Response.AddHeader("content-disposition", "attachment; filename = ""XMLLogs.zip""")
    		context.Response.Cache.SetNoServerCaching()
    		context.Response.Cache.SetNoStore()
    		context.Response.Cache.SetMaxAge(System.TimeSpan.Zero)
    		context.Response.Cache.SetCacheability(HttpCacheability.Private)
    
    		Dim oBuffer As Byte() = New Byte(4095) {}
    
    		'create the new output stream and set the level of compression
    
    		Dim oZipOutputStream As New ZipOutputStream(context.Response.OutputStream)
    		oZipOutputStream.SetLevel(3)
    
    		'Loop through each of the files
    
    		For Each sFileName As String In sFileList
    
    			'create the stream
    
    			Dim oFileStream As Stream = File.OpenRead(sFileName)
    			Dim oZipEntry As New ZipEntry(ZipEntry.CleanName(sFileName))
    			oZipEntry.Size = oFileStream.Length
    
    			'put the entry in to the stream
    
    			oZipOutputStream.PutNextEntry(oZipEntry)
    
    			Dim iCount As Integer = oFileStream.Read(oBuffer, 0, oBuffer.Length)
    			While iCount > 0
    				oZipOutputStream.Write(oBuffer, 0, iCount)
    				iCount = oFileStream.Read(oBuffer, 0, oBuffer.Length)
    				If Not context.Response.IsClientConnected Then
    					Exit While
    				End If
    			End While
    			oFileStream.Close()
    		Next
    
    		oZipOutputStream.Close()
    
    		context.ApplicationInstance.CompleteRequest()
    
    		'delete the files now that we are done with them to not clog up the server
    		'Dim oFolder As New DirectoryInfo(Chop(HttpContext.Current.Session("XMLLogsFolder").ToString))
    		'oFolder.Delete(True)
    
    	End Sub
    
    


     

    Tuesday, July 24, 2012 10:19 AM

All replies

  • User-1002157272 posted

    what compression library are you using to zip the file? Is it one that is natively supported by .NET?

    Tuesday, July 24, 2012 12:27 PM
  • User1315518678 posted

    It's Sharp Zip. 

    The problem isn't with the actual generation of the file, as this is actually happening and happens when you click Retry, so I'm fairly sure it will be one of the settings on the context.Response.

    I'm all ears to any other suggestions though. 

    Tuesday, July 24, 2012 12:31 PM