?
Solved

context.Response.Write MemoryStream

Posted on 2010-08-17
1
Medium Priority
?
1,178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses

770 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