下載zip文件與webclient似乎不能正常工作,一旦下載並保存壓縮文件出現無效或損壞,用zip壓縮閱讀器打開。然而,源zip文件似乎很好,它是一個有效的zip。c#WebClient下載zip破壞它
下載代碼:
using (WebClient webClient = new WebClient())
{
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri(URL), downloadZipFilename);
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
//unzip
using (ZipFile zipFile = ZipFile.Read(currentTemporaryDownloadFileUrl))
{
zipFile.ExtractAll(currentTargetFileUrl);
}
File.Delete(currentTemporaryDownloadFileUrl);
DownloadFinished(this,EventArgs.Empty);
Console.WriteLine("File finished downloading.");
}
拉鍊提取出來作爲腐敗。
Server代碼:
//send file
e.Response.Connection.Type = HttpServer.Headers.ConnectionType.Close;
byte[] buffer = ReadFile(filePath);
e.Response.Body.Write(buffer, 0, buffer.Length);
READFILE服務器上:
public static byte[] ReadFile(string filePath)
{
// this method is limited to 2^32 byte files (4.2 GB)
FileStream fs = File.OpenRead(filePath);
try
{
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
fs.Close();
return bytes;
}
finally
{
fs.Close();
}
}
這裏有什麼錯?
感謝, 基督教斯圖爾特
您可能在'ReadFile'中耗盡內存。 –
您是否將下載的文件與服務器上的文件進行了比較? –
任何想法?任何人 –