【问题标题】:downloaded zip file using c# code is invalid使用 c# 代码下载的 zip 文件无效
【发布时间】:2012-09-03 06:59:18
【问题描述】:

我有一个存储一些 zip 文件的 Dot Net MVC 服务器。如果单击超链接,我可以成功下载这些 zip 文件。但是,如果我尝试使用 WebClient 的 DownloadFile 下载 zip 文件,我可以下载 zip 文件我收到错误“Windows 无法打开文件夹,压缩的 zip 文件夹无效”

服务器端代码:

public FilePathResult DownloadFile(int id)
{      
       string resultsdir = AppDomain.CurrentDomain.BaseDirectory + "Data\\ResultsDir\\" + res.RequestId.ToString();
       string downloadFile = System.IO.Path.GetFileName(res.DownloadPath);
       string zipPath = System.IO.Path.Combine(resultsdir, downloadFile);
       return File(zipPath, "application/zip", downloadFile);
}

客户端我使用Webclient来下载这个文件

WebClient wc = new WebClient();
wc.DownloadFile("http://servername/Results/DownloadFile/853", "localspkgfile.zip");

如果我通过单击浏览器上的超链接下载文件,文件大小为 2.9 mb。但是使用 webclient 文件大小为 5kb。看起来 WebClient 无法正确下载文件。谁能给我一个下载文件的方法。

【问题讨论】:

    标签: c# c asp.net-mvc webclient-download


    【解决方案1】:

    我不知道您的代码有什么问题,但您下载的那个 5kb 文件几乎可以肯定是一个 HTML 错误页面,其中可能包含信息,例如堆栈跟踪,可以帮助您找出问题所在.将其扩展名更改为 .html 并在浏览器中打开。

    【讨论】:

      【解决方案2】:

      我也遇到了这个问题,但是我找到了一个简单的解决方案 我想从“http://sodco.ir/Choobkhat/Update/update.zip”下载这个 Zip 文件 并提取它们。但是 webClient 没有下载它。

      我的解决方案:

      第 1 步: 将我的文件名从“D:\update.zip”更改为“D:\update.zi”webClient.DownloadFileAsync("http://sodco.ir/Choobkhat/Update/update.zip", "D:\\update.zi);

      会开始下载,下载完成后:

      第 2 步: 将 update.zi 重命名为 update.zip

      File.Move("update.zi", "update.zip");

      第 3 步:提取它们

      ZipFile.ExtractToDirectory(Application.StartupPath + "\\update.zip", Application.StartupPath);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-28
        • 1970-01-01
        • 1970-01-01
        • 2022-06-26
        • 2021-12-08
        相关资源
        最近更新 更多