【问题标题】:C# - Cannot Open Dowloaded RAR or ZIP FileC# - 无法打开下载的 RAR 或 ZIP 文件
【发布时间】:2020-06-17 22:13:05
【问题描述】:

需要您的帮助。 当我下载自己打开但当我用 c# windows 下载它时下载成功但没有打开, 我正在使用此代码下载 Rar 或 Zip 文件。

private void downloadBtn_Click(object sender, EventArgs e) 
{
    WebClient client = new WebClient();
    string tAddress = "Download Link"; // When i Downloaded mySelf it Worked And Opened
    string fileName = "Testfile.Zip";
    Uri uri = new Uri(tAddress);
    client.DownloadFileCompleted += Client_DownloadFileCompleted;
    client.DownloadFileAsync(uri, fileName);

}
private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
    MessageBox.Show("Download Completed Successfully!");
}

这是我尝试打开 Rar 或 Zip 文件时出现的错误。 It Says Error, This File Has Incorrect Format Or Damaged, But When I Try To Download Myself Its Opened

【问题讨论】:

  • 您自己下载文件与使用此代码下载文件的大小相比如何?
  • 你如何打开文件以及它在哪里完成我没有看到任何await 你只是缺少await client.DownloadFileAsync(uri, fileName); 否则更改为client.DownloadFile(uri, fileName);
  • @Code Stranger,当我自己下载时,大小为 3MB,代码为 150kb,我不知道发生了什么\
  • @Seabizkit 当我尝试await client.DownloadFileAsync(uri, fileName)await client.DownloadFile(uri, fileName) 它给我带来错误,你能给我代码怎么做吗?
  • @GuKkA 确认它有效,它只是client.DownloadFile(uri, fileName) 而不是await client.DownloadFile(uri, fileName) 关键字await 用于异步方法......所以如果有效,请尝试client.DownloadFile(uri, fileName)。 .那你可以研究Async

标签: c#


【解决方案1】:

该网站不友好也许是故意的,但谁知道呢

如果你按照他在这里做的事情,它会起作用..

Why doesn't WebRequest.AllowAutoRedirect redirect the initial url to the correct download url?

【讨论】:

  • 我要测试解决方案
【解决方案2】:

使用 try catch 块来发现它抛出的异常:

try
{
    WebClient client = new WebClient();
        string tAddress = "Download Link"; // When i Downloaded mySelf it Worked And Opened
        string fileName = "Testfile.Zip";
        Uri uri = new Uri(tAddress);
        client.DownloadFileCompleted += Client_DownloadFileCompleted;
        client.DownloadFileAsync(uri, fileName);
}
catch(Excepction ex)
{
    MessageBox.Show(ex.Message)
}

在这种情况下,问题可能出在 Client.DownloadFileAsync() 中,请使用方法:“ZipFile.Open” 另外,对于 rar 试试这个:https://stackoverflow.com/a/11523864/12764752

【讨论】:

  • 同样的问题下载但无法打开。
猜你喜欢
  • 1970-01-01
  • 2016-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多