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

Forwarding WebResponse File To Web Browser

Hello everyone,

I have read through the newsgroups (thank God for 'em!) extensively looking
for an example of how to pass a file (PDF) from a webresponse stream down to
a web client. Here's the scenario - when the web browser requests a file,
the IIS Server1 has to go back to Server2 to make a request for the file.
Server2 then responds the file back to Server1 and Server1 should be able to
just forward the file down to the web browser. Can someone give me some of
the nitty gritty of how I can properly form the final response from Server1
down to the web browser? I have no trouble getting the response back from
Server2 to Server1. I just now need to format the response properly to get
the file down to the web browser. Many thanks in advance for your help

FYI, all I am looking for is how to format the response. If you want to
give me a code sample, you can assume that I have the WebResponse and start
from there. Again...thanks!!

Jeff G.
Nov 18 '05 #1
2 3371
One other thing - I am trying not to have to write this file to disk before
I send it down. Would like to forward it on down directly out of memory.
Thanks again for reading and responding!

Jeff G.
Nov 18 '05 #2
Ok...I have worked up something after several days of work...hope this helps
someone else. What I found was that I needed to use a Binary Reader to
handle writing the file to the OutputStream. Check it out.

// Put the webResponse in a stream
Stream stream = webResponse.GetResponseStream();

// Setup the content disposition to ensure that the file "Open or Save"
dialog is popped in the browser
string contentDisposition =
webResponse.Headers["Content-Disposition"].ToString();
contentDisposition = contentDisposition.Replace("inline", "attachment");
try
{
// Change the Headers
System.Web.HttpContext.Current.Response.ClearHeade rs();
// Write the type of file we are downloading
System.Web.HttpContext.Current.Response.ContentTyp e =
pdmResponse.Headers["Content-Type"].ToString(); //"application/pdf";
// Assign a file name and the way that the file will be downloaded
System.Web.HttpContext.Current.Response.AddHeader( "Content-Disposition",
contentDisposition);
// Since we know we are getting a Binary File we need a BinaryReader to
take in the stream
BinaryReader binaryReader = new BinaryReader(stream);
// Read the stream via the BinaryReader into a byte buffer
byte[] buffer = binaryReader.ReadBytes((int)webResponse.ContentLen gth);
// Write that buffer into the Output stream
System.Web.HttpContext.Current.Response.OutputStre am.Write(buffer, 0,
(int)webResponse.ContentLength);

// Make the flush call send down the file.
System.Web.HttpContext.Current.Response.Flush();

}
catch(Exception e)
{
throw new ApplicationException("Failed to put file into output stream.",
e);
}

Jeff G.
"Jeff G." <je**@jeff.com> wrote in message
news:uT**************@TK2MSFTNGP10.phx.gbl...
Hello everyone,

I have read through the newsgroups (thank God for 'em!) extensively looking for an example of how to pass a file (PDF) from a webresponse stream down to a web client. Here's the scenario - when the web browser requests a file,
the IIS Server1 has to go back to Server2 to make a request for the file.
Server2 then responds the file back to Server1 and Server1 should be able to just forward the file down to the web browser. Can someone give me some of the nitty gritty of how I can properly form the final response from Server1 down to the web browser? I have no trouble getting the response back from
Server2 to Server1. I just now need to format the response properly to get the file down to the web browser. Many thanks in advance for your help

FYI, all I am looking for is how to format the response. If you want to
give me a code sample, you can assume that I have the WebResponse and start from there. Again...thanks!!

Jeff G.

Nov 18 '05 #3

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

Similar topics

1
by: Binod Nair | last post by:
Hi All, I have an ASP.NET appication running on http://xx.xxx.xxxx.xx/aspApplication and I have a domain http://www.mydomain.com registered at godaddy.com for this application.I have setup...
0
by: Johann Blake | last post by:
In my need to decode a JPEG 2000 file, I discovered like many that there was no functionality for this in the .NET Framework. Instead of forking out a pile of cash to do this, I came up with the...
2
by: gizmo | last post by:
Hi, I'm using the following code to request the html source from the quoted site. ...... string url = "http://www1.soccerstand.com/"; WebRequest webRequest = WebRequest.Create(url);...
2
by: Randy | last post by:
How do I get the contents of the HttpWebResponse to display in a web browser (IE or browser control)? Here is a code snippet. The response ends up being jscript with a window.open(). ...
6
by: Brent | last post by:
I'm having odd problems with the WebResponse class. Some servers are speedy, while others don't play along at all. Consider the following pages*: ...
1
by: thomas | last post by:
Hello all, It seems like subdomain forwarding prevents ASP.Net session state from working correctly. Example: two websites http://www.jgphotographers.com/test and...
6
by: mcl | last post by:
I have a domain name which is set up for web forwarding with a frame. I have a link on one of the site's pages to an external site. When I select the link the external site is displayed correctly...
1
by: vito16 | last post by:
Hi, I have some C# code for a console application that was correctly grabbing pages until recently were the data is now incomplete. I am needing to grab all information including sponsored links...
4
by: CindyH | last post by:
Hi - hope someone can help with this - this code was working for a while in the 'real' code and then suddenly stopped - not sure what happen. I made two simple forms on localhost to try to test...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.