problems with MemoryStream.WriteTo(Response.OutputStream)

Z

Zoury

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
' 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 !
 
Z

Zoury

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
 
O

Oliver Wong

Zoury said:
I managed to do it by adding calling the Close() method on the Response
property :


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
 
Z

Zoury

See the MSDN API documentation for StreamWriters:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top