Downloading a file in ASP.NET (through the server) while streaming it to the user

Posted by James Teare on Stack Overflow See other posts from Stack Overflow or by James Teare
Published on 2012-06-09T16:34:49Z Indexed on 2012/06/09 16:40 UTC
Read the original article Hit count: 346

Filed under:
|
|
|

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;
        }
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET