简体   繁体   English

WebClient 下载损坏的文件

[英]WebClient downloads corrupted files

I'm trying to download a Graph.zip file from a certain URL: https://www.dropbox.com/s/erc6ke9k1x2nle0/Graph.zip Using this code: I'm trying to download a Graph.zip file from a certain URL: https://www.dropbox.com/s/erc6ke9k1x2nle0/Graph.zip Using this code:

WebClient webClient = new WebClient();
webClient.DownloadFile("https://www.dropbox.com/s/erc6ke9k1x2nle0/Graph.zip",
                        @"Graph.zip");

Whenever I execute the code, its either corrupted and/or 0 bytes in size.每当我执行代码时,它要么已损坏和/或大小为 0 字节。 Am i doing something wrong?难道我做错了什么?

Try to open the url from your browser: https://www.dropbox.com/s/erc6ke9k1x2nle0/Graph.zip尝试从浏览器打开 url: https://www.dropbox.com/s/erc6ke9k1x2nle0/Graph.zip

You'll notice that you are redirected to an intermediary page.您会注意到您被重定向到一个中间页面。 If you save it to a zip file extention, then since the file is not really zip, it appears as corrupted to zip clients.如果将其保存到 zip 文件扩展名,则由于该文件不是真正的 zip,因此在 zip 客户端看来它已损坏。 To download dropbox files, you either need to use the rest api, or the sdk.要下载保管箱文件,您需要使用 rest api 或 sdk。

Check the sample console application here for an example: https://www.dropbox.com/developers/documentation/dotnet#tutorial在此处查看示例控制台应用程序以获取示例: https://www.dropbox.com/developers/documentation/dotnet#tutorial

async Task Download(DropboxClient dbx, string folder, string file)
{
    using (var response = await dbx.Files.DownloadAsync(folder + "/" + file))
    {
        Console.WriteLine(await response.GetContentAsStringAsync());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM