2

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;
        }
    }
James Teare
  • 372
  • 1
  • 12
  • 23
  • here is the same q/answer: http://stackoverflow.com/questions/5596747/download-stream-file-from-url-asp-net/5605490 – Aristos Jun 09 '12 at 17:13

1 Answers1

0

If i understood correctly, you are implementing some sort of caching for the remote files you are downloading, yet streaming to the user simultanously. In that case, you probably don't want to use WebClient and wait for the entire file to complete.

Check out the answer to a similar post, using HttpWebRequest: https://stackoverflow.com/a/5605490/1373170

In that scenario, you can go streaming the file in chunks and send those same chunks both to local storage and to the Response. If you don't really need the file locally on the server you can avoid that storage altogether, and only stream it to the user directly.

Community
  • 1
  • 1
Pablo Romeo
  • 11,298
  • 2
  • 30
  • 58