473,382 Members | 1,791 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,382 software developers and data experts.

HttpResponse in asp.net

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?
Jan 26 '07 #1
4 2427

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();
Jan 26 '07 #2
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.


Jan 29 '07 #3
Do I need Response.End?

"Steven Cheng[MSFT]" wrote:
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.


Jan 30 '07 #4
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.

Jan 31 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Adrian Albisser | last post by:
Hey to everybody, im just beginning to program in python. So i was trying some function but always when i try the range function i get a error message. for number in range(1,100): print number...
0
by: James Thurley | last post by:
I'm creating an XmlDocument manually, adding content using the Xml classes such as XmlElement and XmlText, and I then write it out as as "text/xml" to the HttpResponse.Output TextWriter object...
2
by: Kavvy | last post by:
Hi, Can anyone tell me why the following code produces the error "An object reference is required for the nonstatic field, method, or property 'System.Web.HttpResponse.Redirect(string)'"
0
by: Matthew Trunnell | last post by:
Hi everyone, I'm implementing the HTTP 206 Partial Content spec. This requires an additional header of the form: "Content-Range: xxx-yyy/zzz" See http://www.w3.org/Protocols/rfc2616/rfc2616-...
1
by: James Dixon | last post by:
I am hitting a j2ee website that is using cookies to maintain session state. In the headers, it has the syntax Set-Cookie:JSESSIONID=XXXXXXXXXXX When I inspect the HTTPResponse.Cookies collection,...
1
by: Chris Simmons | last post by:
Hello: I am trying to better understand the HttpResponse.Filter property and, although I think I "get it," I am wondering what is going on behind the scenes. I was initially stumped with the...
4
by: Paul | last post by:
I have developed an ASP.NET web page with a VB.net for the code behind. I would like to redirect the output of the web page so I can send it as an Email. Or Redirect the HTTPResponse stream on...
5
by: Daniel Corbett | last post by:
I am trying to save a file dynamically created in a webpage. I get the following headers, but cannot figure out how to save the attachment. I am basically trying to replicate what internet...
2
by: Vadim | last post by:
Hi, I will have to send a file to a user's browser to be opened in a Save, Open manner, I am using HttpResponse.WriteFile for this, the info to be sent will have to be first accumulated in a...
2
by: somoza.albert | last post by:
Hello, I've been told that I can make my Web Pages render quicker if I set the BufferOutput property of the HttpResponse Class to false. I have a "test" Web App that works, but it doesn't seem...
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: 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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.