DownloadFileAsync下载损坏的文件

时间:2019-11-09 01:09:51

标签: c# webclient

我正在尝试构建下载类。从GitHub上下载的文件很好,但是如果我下载ROBLOX资产(例如,http://www.roblox.com/asset/?id=1286103),则当它下载以标题“ 1.00版本”开头的ROBLOX网格文件时,它将下载一个填充有垃圾数据的文件。

    public void InitDownload(string additionalText = "")
    {
        downloadOutcomeAddText = additionalText;

        saveFileDialog1 = new SaveFileDialog()
        {
            FileName = fileName,
            //"Compressed zip files (*.zip)|*.zip|All files (*.*)|*.*"
            Filter = fileFilter,
            Title = "Save " + fileName
        };

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                using (WebClient wc = new WebClient())
                {
                    wc.DownloadProgressChanged += wc_DownloadProgressChanged;
                    wc.DownloadFileAsync(new Uri(fileURL), saveFileDialog1.FileName);
                }

                downloadOutcome = "File " + Path.GetFileName(saveFileDialog1.FileName) + " downloaded!" + downloadOutcomeAddText;
            }
            catch (Exception ex)
            {
                downloadOutcome = "Error when downloading file: " + ex.Message;
            }
        }
    }

    void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        downloadProgress.Value = e.ProgressPercentage;
    }

1 个答案:

答案 0 :(得分:0)

ROBLOX API似乎正在使用gzip编码的数据进行响应,显然WebClient.DownloadFileAsync并未默认自动解压缩响应主体。有关更多信息以及如何处理此问题的过时示例,请参见:https://blog.codinghorror.com/netwebclient-and-gzip/

更新:这个堆栈溢出的答案也可能会有所帮助(它更简单,并且不需要第三方库):Uncompressing gzip response from WebClient