Download zip file from a URL

Asked

Viewed 294 times

3

Good evening, I need to download a zip file and save to a server, however, with the code I am using below, it just creates the zip file on the server; when opening it, there is no file in it and is given the following message: Unsupported ARCHIVE type.

Inside this zip file, we have two json files. By calling the URL by the browser, it automatically downloads.

            var localPath = @"\\nomeServidor\\nomepasta\\" + nomeArquivo;

            WebClient webClient = new WebClient();
            webClient.DownloadFileAsync(new Uri(link), localPath);

            while (webClient.IsBusy) { }

            return Request.CreateResponse(HttpStatusCode.OK);

Could someone help me in possible solutions?

1 answer

3


You don’t have to do this gambit with while, you can use the downloadfile without being async, because you don’t need it async.

In case your code would be:

  var localPath = @"\\nomeServidor\\nomepasta\\" + nomeArquivo;
  using (var client = new WebClient())
  {
    client.DownloadFile(localPath);
  }

This code should work because as far as I know the downloadfile does not distinguish from file type. If it does not work, make sure it is not a problem in the downloaded file or if the user running the program has permissions to access localPath.

  • Paul, I changed the code, but unfortunately it didn’t work. I am running location on my machine and have access to folder, however it writes an empty zip file.

  • Try to require administrator permissions to run your program. To do this, add the following line in your application’s manifest: <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />&#xA;

  • Still nothing.. :/

  • What error? could give more information? Can you download the file normally through the browser?

  • There is no error in the application. By the browser, the file is downloaded normally, with its correct content. By the application, a zip file is created only without content. It might be something related to link permission?

  • Paulo, problem solved rsrs It really was the authentication. So beast that you can’t believe kkkkk Thank you!

  • It was nothing, can you mark the answer as right? It helps the community :)

Show 2 more comments

Browser other questions tagged

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