使用C#代码下载的zip文件无效 [英] downloaded zip file using c# code is invalid

查看:146
本文介绍了使用C#代码下载的zip文件无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Dot Net MVC server that stores some zip files. I'm able to download these zip files successfully if I click on the hyperlink. However if I try to download the zip file using WebClient's DownloadFile, I'm able to download the zip file I get an error "Windows cannot open the folder, compressed zip folder is invalid"

服务器端代码:

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下载此文件

Client side I'm using Webclient to download this file

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

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

If I download file by clicking hyperlink on browser, the filesize is 2.9 mb. However using webclient the file size is 5kb. Looks like WebClient is not able to download the file properly. Can anyone please suggest me a way to download the file.

推荐答案

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

I don't know what is wrong with your code, but that 5kb file you are downloading is almost certainly an HTML error page, which might contain information, such as a stacktrace, that will help you figure out what is going wrong. Change its extension to .html and open it in your browser.

这篇关于使用C#代码下载的zip文件无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆