context.Response.Write MemoryStream

Posted on 2010-08-17
Last Modified: 2012-05-10
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 extension from, which creates a zip file.

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

        Dim outStream As New MemoryStream

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...


Any ideas what Im doing wrong with the:-
        Dim outStream As New MemoryStream

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



    End Sub

End Class

Open in new window

Question by:tonelm54
1 Comment
LVL 96

Accepted Solution

Bob Learned earned 500 total points
ID: 33454605
The preferred method, with ASP.NET, is to write to the OutputStream.

HttpResponse.OutputStream Property

<%@ Page Language="VB" %>

<%@ import Namespace="System.Drawing" %>

<%@ import Namespace="System.Drawing.Imaging" %>

<%@ import Namespace="System.Drawing.Drawing2D" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


<script runat="server">

   Private Sub Page_Load(sender As Object, e As EventArgs)

      ' Set the page's content type to JPEG files

      ' and clears all content output from the buffer stream.

      Response.ContentType = "image/jpeg"


      ' Buffer response so that page is sent

      ' after processing is complete.

      Response.BufferOutput = True

      ' Create a font style.

      Dim rectangleFont As New Font( _

          "Arial", 10, FontStyle.Bold)

      ' Create integer variables.

      Dim height As Integer = 100

      Dim width As Integer = 200

      ' Create a random number generator and create

      ' variable values based on it.

      Dim r As New Random()

      Dim x As Integer = r.Next(75)

      Dim a As Integer = r.Next(155)

      Dim x1 As Integer = r.Next(100)

      ' Create a bitmap and use it to create a

      ' Graphics object.

      Dim bmp As New Bitmap( _

          width, height, PixelFormat.Format24bppRgb)

      Dim g As Graphics = Graphics.FromImage(bmp)

      g.SmoothingMode = SmoothingMode.AntiAlias


      ' Use the Graphics object to draw three rectangles.

      g.DrawRectangle(Pens.White, 1, 1, width - 3, height - 3)

      g.DrawRectangle(Pens.Aquamarine, 2, 2, width - 3, height - 3)

      g.DrawRectangle(Pens.Black, 0, 0, width, height)

      ' Use the Graphics object to write a string

      ' on the rectangles.

      g.DrawString("ASP.NET Samples", rectangleFont, SystemBrushes.WindowText, New PointF(10, 40))

      ' Apply color to two of the rectangles.

      g.FillRectangle( _

          New SolidBrush( _

              Color.FromArgb(a, 255, 128, 255)), _

          x, 20, 100, 50)

      g.FillRectangle( _

          New LinearGradientBrush( _

              New Point(x, 10), _

              New Point(x1 + 75, 50 + 30), _

              Color.FromArgb(128, 0, 0, 128), _

              Color.FromArgb(255, 255, 255, 240)), _

          x1, 50, 75, 30)

      ' Save the bitmap to the response stream and

      ' convert it to JPEG format.

      bmp.Save(Response.OutputStream, ImageFormat.Jpeg)

      ' Release memory used by the Graphics object

      ' and the bitmap.



      ' Send the output to the client.


   End Sub 'Page_Load


<html xmlns="" >


    <title>ASP.NET Example</title>



    <form id="form1" runat="server">




Open in new window


Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now