首页 > 解决方案 > WebClient.DownloadDataAsync 正在返回损坏的数据

问题描述

我正在尝试将数据从应用程序上传和下载到 Web API 的原型。原型将文件 (test.zip) 上传到服务器。服务器将其作为新文件保存到同一文件夹中,然后再次返回 test.zip,客户端保存接收到的文件。上传工作正常,下载运行没有错误。但是,当将接收到的数据写入文件时,文件大小会膨胀约 30%,并且在我尝试打开它时会损坏。

我试图在网上找到有类似问题的人,但没有找到任何结果。我是这个话题的新手,所以不确定自己要尝试什么。

客户端(控制台应用程序):

static void Main()
{
    ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;
    Upload();
    Console.ReadLine();
}

static void Upload()
{
    var webClient = new WebClient();
    var file = File.ReadAllBytes("C:/Users/LPQ/Downloads/Working Folder/test.zip");
    webClient.UploadDataCompleted += (s, e) => Download(webClient);
    webClient.UploadDataAsync(new Uri("localhost/API/APISync/Upload"), file);
}

static void Download(WebClient webClient)
{
    webClient.DownloadDataCompleted += (s, e) =>
    {
        File.WriteAllBytes("C:/Users/LPQ/Downloads/Working Folder/test_SentToClient.zip", e.Result);
        Console.WriteLine("Done");
    };
    webClient.DownloadDataAsync(new Uri("localhost/API/APISync/Download"));
}

服务器

[HttpPost]
[Route("API/APISync/Upload")]
public async Task Upload()
{
    var fileContents = await Request.Content.ReadAsByteArrayAsync();
    File.WriteAllBytes("C:/Users/LPQ/Downloads/Working Folder/test_SentToServer.zip", fileContents);
}

[HttpGet]
[Route("API/APISync/Download")]
public byte[] Download()
{
    return File.ReadAllBytes("C:/Users/LPQ/Downloads/Working Folder/test.zip");
}

text.zip 为 30,837 KB。

上传会生成一个名为 test_SentToServer.zip 的文件,大小为 30,837 KB。

下载会生成一个名为 test_SentToClient.zip 的文件,但它是 41,116 KB,无法打开(损坏)。

显然,我希望客户端收到的文件再次为 30,837 KB。

结果截图

标签: c#asp.netwebclient

解决方案


经过一个下午拔头发后终于解决了这个问题,这很简单。

在 WebClient.DownloadComplete 事件处理程序中,它被更改为从响应中获取正确的文件数据:

var base64String = System.Text.Encoding.Default.GetString(e.Result).Replace("\"", "");
var fileData = Convert.FromBase64String(base64String);
File.WriteAllBytes("C:/Users/LPQ/Downloads/Working Folder/test_SentToClient.zip", fileData);
Console.WriteLine("Done");

服务器的响应是我的 base64 格式数据的字节数组,而不是我的数据的字节数组。所以需要先转成base64字符串,再转成解码后的字节数组。


推荐阅读