473,714 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems with MemoryStream.Wr iteTo(Response. OutputStream)

Hi folks ! :O)

I'm trying to show a PDF in ASP.NET but I can't get to work properly.

here's a portion of my code :
'**
Dim ms As MemoryStream =
DirectCast(m_re port.FormatEngi ne.ExportToStre am(reqContext), MemoryStream)

If (fileName Is Nothing) Then
' shows the PDF to the user
With Response
.ClearHeaders()
.ClearContent()
.ContentType = "applicatio n/pdf"
ms.WriteTo(.Out putStream)
.Flush()
End With
Else
' writes the PDF on the disk
Dim fs As New FileStream(file Name, FileMode.Create New)
ms.WriteTo(fs)
fs.Close()
End If
'**

If I write the MemoryStream to disk and open the file, it appears correctly.
If I write the MemoryStream into the Response.Output Stream object, i get a
the following message :
---------------------------
Acrobat Reader
---------------------------
The file is damaged and could not be repaired.
---------------------------
OK
---------------------------

I also tried to use Response.Suppre ssContent = True just in case other stuff
was written in the OutputStream but unfortunatly the only thing i got from
doing so was a blank page (and no alert anymore).

Any ideas of what's happening ?
Thanks a lot !

--
Best Regards
Yanick Lefebvre
Nov 19 '05 #1
3 1920
I managed to do it by adding calling the Close() method on the Response
property :
With Response
.ClearHeaders()
.ClearContent()
.ContentType = "applicatio n/pdf"
ms.WriteTo(.Out putStream)
.Flush() .Close() End With

I'm quite new to ASP.NET so forgive my ignorance... but why do we have to
close the socket connection ? Is there a new connection created on each
postback ?
Thanks again

--
Best Regards
Yanick Lefebvre
Nov 19 '05 #2

"Zoury" <ya************ *@hotmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
I managed to do it by adding calling the Close() method on the Response
property :
With Response
.ClearHeaders()
.ClearContent()
.ContentType = "applicatio n/pdf"
ms.WriteTo(.Out putStream)
.Flush()

.Close()
End With

I'm quite new to ASP.NET so forgive my ignorance... but why do we have to
close the socket connection ? Is there a new connection created on each
postback ?
Thanks again


See the MSDN API documentation for StreamWriters:

Flushing the stream will not flush its underlying encoder unless you
explicitly call Flush or Close. Setting AutoFlush to true means that data
will be flushed from the buffer to the stream, but the encoder state will
not be flushed. This allows the encoder to keep its state (partial
characters) so that it can encode the next block of characters correctly.
This scenario affects UTF8 and UTF7 where certain characters can only be
encoded after the encoder receives the adjacent character or characters.

Basically, the last few bytes were probably not being written to the
file, because the StreamWriter wasn't sure if you were going to write more
data (which would have affected the the bytes still in the buffer) or not.

- Oliver
Nov 19 '05 #3
> See the MSDN API documentation for StreamWriters:
< snip >
Basically, the last few bytes were probably not being written to the
file, because the StreamWriter wasn't sure if you were going to write more
data (which would have affected the the bytes still in the buffer) or not.


Thanks Oliver. I think i understand a bit more the correlation between the
streams, the socket and HTTPResponse object.

--
Best Regards
Yanick Lefebvre
Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
4747
by: TinyTim | last post by:
I'm a newbie at ASP & HTML. It seems that when you use server side code and you're going to return a customized HTML form with several fields and labels, you have to do an extensive amount of Response.Writes. Are there any tools that will let you design the form then convert that form to Response.Writes that you can further customize with ASP logic? For instance: use Dreamweaver to design the form, then another program to convert to...
3
13775
by: Zoury | last post by:
Hi folks ! :O) I'm trying to show a PDF in ASP.NET but I can't get to work properly. here's a portion of my code : '** Dim ms As MemoryStream = DirectCast(m_report.FormatEngine.ExportToStream(reqContext), MemoryStream) If (fileName Is Nothing) Then
2
2966
by: Charles.Deisler | last post by:
I'm reading a xmldocument in from the file system and using xmldocument.writeto(xmltextwriter) to stream the xml data to the response.outputstream. From what i've read, reading an xmldocument is thread safe but does the writeto() method require a lock? Thanks in advance!
3
5078
by: Jenny | last post by:
Hi all, I'm using a second page to write dynamical generated images into the outputstream. This avoids using tmp-files on disc. My code-behind in the start aspx file is: 'Use second file to write into outputstream Application("grafix-obj") = ob button.ImageUrl = "Grafix.aspx"
3
5873
by: T. Davis | last post by:
In C#, I am able to successfully stream a TIFF image that comes from a BLOB field in a database, save it to file, then convert the pages within TIFF file into jpegs (using GDI+) and display on the web (using ASP.NET). However, when I generate the Image object using FromStream (passing in the MemoryStream containing image bytes), an exception of "A generic error occurred in GDI+" is thrown when performing the conversion/save for display....
4
11740
by: Detlef Huettenbach | last post by:
I was trying to convert a Windows Forms prototype application to an ASP.NET solution that makes use of loading data streams into the Image Web/Windows control. For WinForms no problem. However in ASP.NET, the image control does not display tiffs,pngs,.. still it does display gif-streams. Memory stream writing into a gif- stream of the tiff and other streams likewise fails, although the streams seem to be filled correctly. On top of that,...
0
1159
by: alec maclean | last post by:
Hi, I'm trying to write a set of charts (as bitmap objects) out to a memory stream so that a variable number of charts can be displayed using just one <img> html tag in a holder page. I have everything working except that only one chart is ever displayed, no matter how many are actually created. The vb codebehind is called by being placed as the source for an img tag in a holder page. The holder
1
2903
by: Alec MacLean | last post by:
Hi, I'm trying to write a set of charts (as bitmap objects) out to a memory stream so that a variable number of charts can be displayed using just one html <img> tag in a holder page. I have everything working except that only one chart is ever displayed, no matter how many are actually created. The vb codebehind is called by being placed as the source for an img tag in a holder page. The holder page also provides a filter for the...
7
2397
by: Robson Carvalho Machado | last post by:
Does anyone knows how to CAST this SQL Response into a MemoryStream ?? When executing below code an error message says "Specified cast is not valid" I need to put this into MemoryStream to use it into imgPhoto Dim imgPhoto As System.Drawing.Image = System.Drawing.Image.FromStream(myStream) Dim dbConn, SQLStmt, dbComm, dbRead, img Dim myStream 'As New MemoryStream dbConn = New
1
5824
by: MSwanston | last post by:
Hi I need some help with saving retreiving data from the cache, and how best to structure my code. FYI am working in VS2005/under .NET2 Framework. Ok, we have a series of reports that get run via a report filter screen, and whilst each report is being generated, it populates a memorystream/streamwriter. This has always been done this way but I am trying to update/improve and generally speed things up. The variables are declared as follows:...
0
8801
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9174
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9015
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7953
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5947
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4464
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.