[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

context.Response.Write MemoryStream

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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

656 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