469,117 Members | 1,366 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,117 developers. It's quick & easy.

File Download

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.
Jan 13 '06 #1
6 2415

"Jatin" <Ja***@discussions.microsoft.com> wrote in message
news:D6**********************************@microsof t.com...
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.

Jan 13 '06 #2
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" <Ja***@discussions.microsoft.com> wrote in message
news:D6**********************************@microsof t.com...
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.

Jan 13 '06 #3
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" wrote:
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" <Ja***@discussions.microsoft.com> wrote in message
news:D6**********************************@microsof t.com...
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.


Jan 13 '06 #4
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" <Ja***@discussions.microsoft.com> wrote in message
news:D6**********************************@microsof t.com...
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.

Jan 13 '06 #5
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" wrote:
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" <Ja***@discussions.microsoft.com> wrote in message
news:D6**********************************@microsof t.com...
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.


Jan 13 '06 #6
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();
}
}
"Jatin" wrote:
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.

Jan 13 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Buddy Ackerman | last post: by
4 posts views Thread by Nathan Sokalski | last post: by
3 posts views Thread by tshad | last post: by
35 posts views Thread by keerthyragavendran | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.