Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

context.Response.Write MemoryStream

Good afternoon,
Ive written a simple Handeler page, which I hoped would be able to get the user to download a ZIP file without having save the file on the hard drive.

Im using the Ionic.zip extension from http://dotnetzip.codeplex.com/, which creates a zip file.

The zip file creation seems to work fine, and after the line "myZipFile.Save("C:\New folder (2)\outSig.zip")" is run, I can see the zip file and open it fine if I comment out the following lines:-

        Dim outStream As New MemoryStream
        myZipFile.Save(outStream)
        context.Response.Write(outStream)

If I run with the above lines uncommented then the page failes saying:-
Invalid at the top level of the document. Error processing resource 'http://localhost:58053/Handler1.ashx'. Line 1, Positi...

System.IO.MemoryStream
^


Any ideas what Im doing wrong with the:-
        Dim outStream As New MemoryStream
        myZipFile.Save(outStream)
        context.Response.Write(outStream)

Any help would be appriciated.

Thank you
Imports System.IO
Imports Ionic.Zip

Public Class Handler1
    Implements System.Web.IHttpHandler

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        context.Response.ContentType = "application/zip"

        Dim myZipFile As New ZipFile

        Dim strText As String = "This is some sample text"


        myZipFile.AddEntry("default.htm", "\", strText)

        myZipFile.AddFile(context.Request.PhysicalApplicationPath & "logos\myLogo.jpg", "\")

        Dim outStream As New MemoryStream

        myZipFile.Save(outStream)

        context.Response.Write(outStream)
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial