File Download

G

Guest

Hey Guys
I have a web application that allows users to download files. But the files
are not hosted on the webserver. The files are stored on external servers and
are referenced by a URL/URI. Since my application restricts the number of
downloads for the file per user, i cannot show the user the URL/URI of the
server hosting the file.
Therefore i need to some how route the file via my webserver, and then use
the Response to stream the stream to the client.

I have tried to work with the Webclient class, but the only download method
i could use is readopen(others download the file directly to local drive),
which works BUT since it downloads the whole file into a Byte[] which i then
use the response to stream to the client is not the best of ways since the
Byte[] is in memory and the files being downloaded can be large.

Please Help., someway i can use some sort of unbuffered stream to do the
trick.
 
P

Peter Rilling

Jatin said:
Hey Guys
I have a web application that allows users to download files. But the
files
are not hosted on the webserver. The files are stored on external servers
and
are referenced by a URL/URI. Since my application restricts the number of
downloads for the file per user, i cannot show the user the URL/URI of the
server hosting the file.
Therefore i need to some how route the file via my webserver, and then use
the Response to stream the stream to the client.

I have tried to work with the Webclient class, but the only download
method
i could use is readopen(others download the file directly to local drive),
which works BUT since it downloads the whole file into a Byte[] which i
then
use the response to stream to the client is not the best of ways since the
Byte[] is in memory and the files being downloaded can be large.

Please Help., someway i can use some sort of unbuffered stream to do the
trick.
 
P

Peter Rilling

You are one the right track. You can string bytes to the client. If the
file is large, then just read and write chunks. You would want to use the
HttpWebRequest which exposes the source stream. i am not sure but I do not
think that it downloads the entire file. I would imagine that it is
buffered.
 
G

Guest

One method you could use is to create as Web Service on the server hosting
the files. The a request can be made to your public server where an aspx
page will do any necessary processing to check there allowed to download the
requested file or not. The aspx page then set the response header
context-type to text/html, image/gif etc. or whatever you data file is.

Using SAOP you can then connect to your file server and request the file.
This will stream the file across in predefined chunks, which you write
direct to the response buffer and flush the results. You then request the
next chunk through the web service interface until the entire file has been
transferred. The method of requesting the next buffer can be accomplished in
several ways but by far the simplest is to use the current offset. For
example offset of 0 will send the first chunk of the file and you increment
the position by the size of the buffer until the entire file has been
received. This will allow the client side (web server) to specify the size
of the buffer as well so you can control the speed at which the user
receives the file. i.e. The higher the user status the faster you will try
and stream the file to them.

- Mike
 
G

Guest

Hey peter
Need more help:

WebClient client = new WebClient();
Stream st = client.OpenRead(@"http://cre8object.biz.tm/testing/hello.avi");

Apparently this doesnt return me a stream. Also like i earlier said if i
used the client.downloadData methid it returns me a byte array. which is file
since i say stream it down response but the problem is that for large files
the Byte array could be large and in memory....

Can you reiterate on how i would use the httpwebrequest?

Thx and hope to hear from you






Peter Rilling said:
You are one the right track. You can string bytes to the client. If the
file is large, then just read and write chunks. You would want to use the
HttpWebRequest which exposes the source stream. i am not sure but I do not
think that it downloads the entire file. I would imagine that it is
buffered.

Jatin said:
Hey Guys
I have a web application that allows users to download files. But the
files
are not hosted on the webserver. The files are stored on external servers
and
are referenced by a URL/URI. Since my application restricts the number of
downloads for the file per user, i cannot show the user the URL/URI of the
server hosting the file.
Therefore i need to some how route the file via my webserver, and then use
the Response to stream the stream to the client.

I have tried to work with the Webclient class, but the only download
method
i could use is readopen(others download the file directly to local drive),
which works BUT since it downloads the whole file into a Byte[] which i
then
use the response to stream to the client is not the best of ways since the
Byte[] is in memory and the files being downloaded can be large.

Please Help., someway i can use some sort of unbuffered stream to do the
trick.
 
G

Guest

Hi Mike
Yea sounds like a good idea. I didnt think of the solution in that
perspective. I think i will keep this solution handy if all fails. I am
reluctant well the client might be reluctant to put up a webservice. so im
going to hold on to this.

Ideally i would really like to just stream it directly. If you see my reply
to the 'Peter' you will see i am trying to get it working with the webclient,
or any other alternative

Thanks a bunch Mike

Mike said:
One method you could use is to create as Web Service on the server hosting
the files. The a request can be made to your public server where an aspx
page will do any necessary processing to check there allowed to download the
requested file or not. The aspx page then set the response header
context-type to text/html, image/gif etc. or whatever you data file is.

Using SAOP you can then connect to your file server and request the file.
This will stream the file across in predefined chunks, which you write
direct to the response buffer and flush the results. You then request the
next chunk through the web service interface until the entire file has been
transferred. The method of requesting the next buffer can be accomplished in
several ways but by far the simplest is to use the current offset. For
example offset of 0 will send the first chunk of the file and you increment
the position by the size of the buffer until the entire file has been
received. This will allow the client side (web server) to specify the size
of the buffer as well so you can control the speed at which the user
receives the file. i.e. The higher the user status the faster you will try
and stream the file to them.

- Mike

---------------------------------------------------------------------------------
http://www.cogitar.net"> Cogitar Software. ( http://www.cogitar.net )
http://www.web-dominion.co.uk Web-Dominion. (Web Design and hosting )
http://www.shop-dominion.com (senery landscape picture gallery)
---------------------------------------------------------------------------------
Jatin said:
Hey Guys
I have a web application that allows users to download files. But the
files
are not hosted on the webserver. The files are stored on external servers
and
are referenced by a URL/URI. Since my application restricts the number of
downloads for the file per user, i cannot show the user the URL/URI of the
server hosting the file.
Therefore i need to some how route the file via my webserver, and then use
the Response to stream the stream to the client.

I have tried to work with the Webclient class, but the only download
method
i could use is readopen(others download the file directly to local drive),
which works BUT since it downloads the whole file into a Byte[] which i
then
use the response to stream to the client is not the best of ways since the
Byte[] is in memory and the files being downloaded can be large.

Please Help., someway i can use some sort of unbuffered stream to do the
trick.
 
G

Guest

Hey guys
Thx guys. I got the download thing working. Here is the code im using for
anyone else with the same problem:


Stream st = null;

WebClient client;

try

{
client = new WebClient();
string filePath = @"http://XYZ/temp/files/COUNT24.AVI";
st = client.OpenRead(filePath);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;
filename=" + filename);
int length;
bool completed = true;
byte[] buffer = new Byte[10000];

while (st.CanRead && completed == true)
{
length = st.Read(buffer, 0, 10000);
Response.OutputStream.Write(buffer, 0, length);
Response.Flush();
buffer = new Byte[10000];
if(length==0)
{
completed = false;
}
}
}
catch
{
//Response.Write("Error : " + ex.Message);
}
finally
{
if (st != null)
{
st.Close();
}
}
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top