Solved

context.Response.Write MemoryStream

Posted on 2010-08-17
1
1,133 Views
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 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

0
Comment
Question by:tonelm54
1 Comment
 
LVL 96

Accepted Solution

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

HttpResponse.OutputStream Property
http://msdn.microsoft.com/en-us/library/system.web.httpresponse.outputstream.aspx


<%@ 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"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

      Response.Clear()



      ' 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

      g.Clear(Color.LightGray)



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

      g.Dispose()

      bmp.Dispose()



      ' Send the output to the client.

      Response.Flush()

   End Sub 'Page_Load



</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

    <title>ASP.NET Example</title>

</head>

<body>

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

    </form>

</body>

</html>

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

9 Experts available now in Live!

Get 1:1 Help Now