473,385 Members | 1,275 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

problems with MemoryStream.WriteTo(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_report.FormatEngine.ExportToStream(re qContext), MemoryStream)

If (fileName Is Nothing) Then
' shows the PDF to the user
With Response
.ClearHeaders()
.ClearContent()
.ContentType = "application/pdf"
ms.WriteTo(.OutputStream)
.Flush()
End With
Else
' writes the PDF on the disk
Dim fs As New FileStream(fileName, FileMode.CreateNew)
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.OutputStream 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.SuppressContent = 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 1905
I managed to do it by adding calling the Close() method on the Response
property :
With Response
.ClearHeaders()
.ClearContent()
.ContentType = "application/pdf"
ms.WriteTo(.OutputStream)
.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****************@TK2MSFTNGP15.phx.gbl...
I managed to do it by adding calling the Close() method on the Response
property :
With Response
.ClearHeaders()
.ClearContent()
.ContentType = "application/pdf"
ms.WriteTo(.OutputStream)
.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
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...
3
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 =...
2
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...
3
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...
3
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...
4
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...
0
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...
1
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...
7
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...
1
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.