472,779 Members | 2,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 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
Aug 3 '05 #1
3 13588
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
Aug 3 '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
Aug 3 '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
Aug 4 '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: 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...
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 =...
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.