Here’s one way to use it to download a URL and save it to a file: (I am using windows 7, therefore no WindowsRT available to me, so I’m also using System.IO.) public static class WebUtils Return File(contentStream, content_type, filename) Var contentStream = await content.ReadAsStreamAsync() // get the actual content stream
HttpResponseMessage response = await client.GetAsync(uriBuilder.ToString()) Using (HttpClient client = new HttpClient())Ĭlient.BaseAddress = new Uri(uriBuilder.ToString()) Ĭ() Ĭ("authorization", access_token) //if anyĬ(new .MediaTypeWithQualityHeaderValue("application/json")) UriBuilder uriBuilder = new UriBuilder() public async Task GetAttachment(int FileID) If you are calling a WebApi to get a file, then from a controller method you can use HttpClient GET request and return file stream using FileStreamResult return type. So, if performance is my utter priority, which approach should I use for download? I would like to be clarified if my above assumption is wrong, and I am open to alternate approach as well. On the other hand, if I use HttpClient, ain't I loading every single byte of a file into memory and then writing it to a local file? If the file size is too large, won't memory overhead be expensive? Which could be avoided if we use WebClient, since it will directly write to local file and not consume system memory. But what if I am not worried about the blocking of thread or use client.DownloadFileAsync() to leverage the async support? Using (Stream streamToReadFrom = await ())įrom my perspective, I can see only one disadvantage in using WebClient, that would be the non async call, blocking the calling thread. Using (HttpResponseMessage response = await client.GetAsync(url)) HttpClient: using (HttpClient client = new HttpClient())
WebClient: WebClient client = new WebClient() Ĭlient.DownloadFile(downloadUrl, filePath)
#Multi segmented download sftp client code#
I am using below code to download file from internet: But I am still not totally convinced and need more inputs. Everywhere, it is suggested to go for HttpClient due to its great async support and other. I have referenced this article and several other articles on the internet. I am trying to download file from a URL and I have to choose between WebClient and HttpClient.