469,275 Members | 1,871 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

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 13153
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jenny | last post: by
4 posts views Thread by Detlef Huettenbach | last post: by
reply views Thread by alec maclean | last post: by
1 post views Thread by Alec MacLean | last post: by
7 posts views Thread by Robson Carvalho Machado | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.