Downloading a file in ASP.NET (through the server) while streaming it to the user
- by James Teare
My ASP.NET website currently downloads a file from a remote server to a local drive, although when users access the site they have to wait for the server to finish downloading the file until they can then download the file from my ASP.NET website.
Is it possible to almost stream the download from the remote website - through my ASP.NET website directly to the user (a bit like a proxy) ?
My current code is as follows:
using (var client = new WebClientEx())
{
client.DownloadFile(downloadURL, "outputfile.zip");
}
WebClient class:
public class WebClientEx : WebClient
{
public CookieContainer CookieContainer { get; private set; }
public WebClientEx()
{
CookieContainer = new CookieContainer();
}
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = CookieContainer;
}
return request;
}
}