?
Solved

context.Response.Write MemoryStream

Posted on 2010-08-17
1
Medium Priority
?
1,192 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

864 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