HttpResponse in asp.net

G

Guest

I am looking for the most correct method of streaming a byte array to a web
browser as a pdf. Anyway, I see a lot of methods for HttpResponse and I don't
know how to properly use them. Here is my code:
Response.ContentType = "Application/pdf";
Response.BinaryWrite(myByteArray);
Response.End();

Specifically, do I need to worry about Buffer (T/F), AppendHeader(),
Clear(), Flush(). Should I use OutputStream instead of BinaryWrite?

My code is working, but I want to know if it is the right way to do this so
that I don't encounter errors down the road, e.g. problems if a file size is
large, or memory leaks.

Also, on Response.End a 'Thread was being aborted' exception that I guess is
expected behavior. I plan to just catch this specific exception and do
nothing with it, is that the way to handle it?
 
M

Microsoft Community

Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=XXX.PDF");

Byte[] data = new Byte[1024];
Int32 readData = ResponseSoapContext.Attachments[0].Stream.Read(data, 0, 1024);

while(readData > 0)
{
Response.BinaryWrite(data);
readData = ResponseSoapContext.Attachments[0].Stream.Read(data, 0, 1024);
}

Response.End();
 
S

Steven Cheng[MSFT]

Thanks for Wyvern's input.

Hi Robin,

As Wyvern has demonstrated, you can first clear the response content and
header. And set the response.ContentType to the "application/pdf" field.
Also, since you're simply writing out binary content(PDF stream), using
Response.BinaryWriteor the OutputStream property won't matter much. The
"OutputStream" property is often used when you want to do some further
customization on the output content. For example, you can create some other
writer(streamWriter, custom writer....) and use them to wrapper the
OutputStream.

If you have any further question on this, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

Yes, when you finish writing out all the binary bytes of your PDF(or other
content) stream, you can simply call Response.End and it will end the
current request and flush out all the content in buffer to client-side.

Please feel free to let me know if you have any other questions.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Top