Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download files from url to local device in .Net Core

Tags:

In .Net 4.0 I used WebClient to download files from an url and save them on my local drive. But I am not able to achieve the same in .Net Core.

Can anyone help me out on this?

like image 822
Ravi Avatar asked Aug 30 '17 10:08

Ravi


2 Answers

WebClient is not available in .NET Core. (UPDATE: It is from 2.0) The usage of HttpClient in the System.Net.Http is therefore mandatory:

using System.Net.Http; using System.Threading.Tasks; ... public static async Task<byte[]> DownloadFile(string url) {     using (var client = new HttpClient())     {          using (var result = await client.GetAsync(url))         {             if (result.IsSuccessStatusCode)             {                 return await result.Content.ReadAsByteArrayAsync();             }          }     }     return null; } 
like image 192
jAC Avatar answered Sep 18 '22 20:09

jAC


WebClient is available from .net core 2.0

var wc = new System.Net.WebClient(); wc.DownloadFile( URL, @"c:\temp\myfile.txt"); 
like image 26
Eduardo Molteni Avatar answered Sep 17 '22 20:09

Eduardo Molteni