问题
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"
Server side code :
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);
}
Client side I'm using Webclient to download this file
WebClient wc = new WebClient();
wc.DownloadFile("http://servername/Results/DownloadFile/853", "localspkgfile.zip");
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.
回答1:
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.
回答2:
I also had this problem, but I found a simple solution i want to Download this Zip File from "http://sodco.ir/Choobkhat/Update/update.zip" and extract them. but webClient not Download it.
My Solution:
Step 1: change my FileName from "D:\update.zip" to "D:\update.zi" webClient.DownloadFileAsync("http://sodco.ir/Choobkhat/Update/update.zip", "D:\\update.zi);
It will start downloading,After the download is completed:
Step 2: Rename update.zi to update.zip
File.Move("update.zi", "update.zip");
Step 3: extract them
ZipFile.ExtractToDirectory(Application.StartupPath + "\\update.zip", Application.StartupPath);
来源:https://stackoverflow.com/questions/12351206/downloaded-zip-file-using-c-sharp-code-is-invalid