0
Hello I have the following situation with that function. And this giving this rejection
Webclient' does not contain a definition for "Downloaddatataskasync" and it was not possible to find any extension method "Downloaddatataskasync" that accepts a first argument of the Webclient type (you are forgetting to use an Assembly directive or reference?
static async Task DownloadAsync(string sUrl, string sNomeArq)
{
Uri uUri = new Uri(sUrl);
// cria uma instância de webclient
using (WebClient cliente = new WebClient())
{
byte[] bytes;
try
{
// Existe também o método DownloadDataAsync
bytes = await cliente.DownloadDataTaskAsync(uUri);
}
catch (WebException we)
{
return;
}
FileStream Stream = new FileStream(@"D:\temp\" + sNomeArq, FileMode.Create);
//Escrevo arquivo no fluxo
Stream.Write(bytes, 0, bytes.Length);
//Fecho fluxo pra finalmente salvar em disco
Stream.Close();
}
}
However I did with a Console application and it worked as follows and it worked. What this wrong someone can help me ?
static async Task Main(string[] args)
{
List<string> lt = new List<string>();
lt.Clear();
lt.Add("http://www.teste.com.br/atualizasgl/arquivos/7226B398B8ADDDCB5D64412B6FEEBB71.zip");
lt.Add("http://www.teste.com.br/atualizasgl/arquivos/7BC630726516F5B9006DC21F98E77D94.zip ");
lt.Add("http://www.teste.com.br/atualizasgl/arquivos/B9E3AF228E5543E8E045C38550B5D124.zip ");
lt.Add("http://www.teste.com.br/atualizasgl/arquivos/2DE19CEE12378ADEC17FDAB3ECB57E92.zip ");
foreach (string sLink in lt)
{
await DownloadAsync(sLink, "Arq" + lt.IndexOf(sLink) + ".zip");
}
//Console.ReadLine();
}
static async Task DownloadAsync(string url, string sNomeArq)
{
Uri uri = new Uri(url);
// cria uma instância de webclient
using (WebClient cliente = new WebClient())
{
// OBTEM O CONTEÚDO DO ARQUIVO
Console.WriteLine($"Downloading {uri.AbsoluteUri}");
cliente.DownloadDataCompleted += Cliente_DownloadDataCompleted;
cliente.DownloadProgressChanged += Cliente_DownloadProgressChanged;
// faz o download da pagina e armazena em um array de bytes
byte[] bytes;
try
{
bytes = await cliente.DownloadDataTaskAsync(uri);
}
catch (WebException we)
{
Console.WriteLine(we.ToString());
return;
}
//Crio o arquivo em disco e um fluxo
FileStream Stream = new FileStream(@"D:\temp\" + sNomeArq, FileMode.Create);
//Escrevo arquivo no fluxo
Stream.Write(bytes, 0, bytes.Length);
//Fecho fluxo pra finalmente salvar em disco
Stream.Close();
}
}