steam url out to a client

  • Thread starter Thread starter pbearne
  • Start date Start date
P

pbearne

Hi

I need to load an url which should return an pdf/doc file or and html
page if file not found I then need to return this file to the
request(client) as ether as a download using
Response.AppendHeader("Content-Disposition", " attachment; filename=" +
fileName); or inline

Effectly I need to make a web page work as a proxy

This is for a document system where some of the file are in a local
store and some of the file are on remote webservice I need to hide the
source location from the users so all they know is the the url of a
document is fietch.aspx?docid=1234 so i can control access etc.

the local file is easy i am using
"Response.TransmitFile(pathAndFileName);" But Ican't find the code to
stream in and response an remote URL

Any pointer / code will help

Paul
 
If you expect the pages to be small, you should be able to just use
something like WebClient.DownloadData, and then just write the array
directly to the response stream. For larger content, you may have to get the
(remote) response-stream and pump the data in chunks to the (local)
response-stream.

You may also wish to consider a simpler model, such as a handler (ashx)
rather than a page (aspx).

Marc
 
Hi Paul,

If it's truly a "Web Service" from which you are downloading the file then
you can simply write it directly to Response.OutputStream after it's
retrieved.
 
What I an expecting is a pdf/doc file if the file is found else I will
get an HTML error page

I do wish to be able to forse the file into dowmload mode ie. havw to
links per file download - view

some of these file will be big so I do need to be carefull not to
overload the server etc.

So It might be safer to write to the file system
 
Hi Paul,
What I an expecting is a pdf/doc file if the file is found else I will
get an HTML error page

It doesn't sound to me like you're using a Web Service at all then :)

It sounds to me like you're simply making an HTTP request for a document
directly to a web server, in which case Marc's solutions are good ones.
I do wish to be able to forse the file into dowmload mode ie. havw to
links per file download - view

It's not clear to me what you want here. Do you mean asynchronous
downloads?
some of these file will be big so I do need to be carefull not to
overload the server etc.

So It might be safer to write to the file system

Well, if you're going to be downloading the complete files into memory
anyway then writing them to disc will actually be more of a burden on the
server. Marc's suggestion about downloading large files in chunks seems
like it might be appropriate for your situation. Just write each chunk to
the Response.OutputStream one at a time. The size of the chunks should
depend on the available resources of the system that is hosting your
application, although you'll probably be able to get away with hard-coding
some size like 0x20000.
 
Hi

I am all ready fetching files from the local file system using

Response.TransmitFile(filePath);

and I eaither set this to make the save as dailog appear
Response.AppendHeader("Content-Disposition", " attachment; filename=" +
fileName);

or this to make the brouser to try and load it
Response.AppendHeader("Content-Disposition", " inline; filename=" +
fileName);

My problems come when the file I want to return is not the local file
but been returned by a remote service

I was just have problems get the streaming to work and have been able
to find any examples of this been done

will WebClient.DownloadData work with DPF/DOC files


Thanks for you help Paul


I do like the idea of change this into a ashx page when it is all
working
 
It makes no difference what the underlying remote response is; it could
be doc, pdf, txt, etc... no different.

Essentially, the following should work, which forwards all headers and
content, without ever holding the entire remote page in memory:

public void ProcessRequest (HttpContext context) {
HttpResponse localResponse = context.Response;
using (WebClient wc = new WebClient())
using (Stream remoteResponseStream =
wc.OpenRead(@"http://groups.google.com"))
using (Stream localResponseStream = localResponse.OutputStream)
{
localResponse.Clear();
WebHeaderCollection remoteHeaders = wc.ResponseHeaders;
foreach (string key in remoteHeaders.AllKeys) {
string header = remoteHeaders.Get(key);
System.Diagnostics.Debug.WriteLine(header, key);
localResponse.AddHeader(key, header);
}
const int BUFFER_SIZE = 2048;
int bytesRead;
long totalBytes = 0;
byte[] buffer = new byte[BUFFER_SIZE];
while((bytesRead = remoteResponseStream.Read(buffer, 0,
BUFFER_SIZE)) > 0) {
localResponseStream.Write(buffer, 0, bytesRead);
totalBytes += bytesRead;
}
System.Diagnostics.Debug.WriteLine(totalBytes,
"*written:");
}
}
 

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

Back
Top