Download Async Task C#

Asked

Viewed 44 times

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

2 answers

1

The method DownloadDataTaskAsync belongs to the class System.Net.WebClient:
https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient.downloaddatataskasync?view=net-5.0

You need to use this class and your code will work:

string url = "http://www.geonames.org/childrenJSON?geonameId=6255150";

// Não usei using, aqui o namespace completo:
using (var wc = new System.Net.WebClient())
{
    var  result = await wc.DownloadDataTaskAsync(url);
    var json = System.Text.Encoding.Default.GetString(result);
    
    Console.WriteLine(json);
}

Can be seen working here: https://dotnetfiddle.net/8GJdvh

In time, I got this endpoint from that other answer: /a/76649/57220

0

I believe the mistake is because you’re forgetting to import some library from C#, as you’re saying in: você está se esquecendo de usar uma diretiva ou uma referência de assembly?, if you are using Visual Studio, just press put cursor on the line where the error is and press Ctrl + ., and if the problem is really lack of library import, Visual Studio will give the suggestion to import the library

Browser other questions tagged

You are not signed in. Login or sign up in order to post.