我有这个方法:
HttpWebRequest request;
void fileDownloadRadar(string uri, string fileName)
{
request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
request.CookieContainer = new CookieContainer();
request.AllowAutoRedirect = true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.ContentType == "")
{
Logger.Write("ContentType is Empty download was not fine !!!!!");
}
if ((response.StatusCode == HttpStatusCode.OK ||
response.StatusCode == HttpStatusCode.Moved ||
response.StatusCode == HttpStatusCode.Redirect) &&
response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
{
Logger.Write("ContentType is not empty meaning download is fine");
using (Stream inputStream = response.GetResponseStream())
using (Stream outputStream = File.OpenWrite(fileName))
{
byte[] buffer = new byte[4096];
int bytesRead;
do
{
bytesRead = inputStream.Read(buffer, 0, buffer.Length);
outputStream.Write(buffer, 0, bytesRead);
} while (bytesRead != 0);
}
FinishWebRequest();
}
else
{
timer1.Stop();
timer3.Start();
}
}
在这个方法里面我想要计算文件下载进度并将其报告给form1 consturctor:
在form1构造函数中,我做了:
fileDownloadRadar(remote_image_on_server, combinedTemp);
splash.UpdateProgressBar(
UpdateProgressBar应该接受int。 在表格中,我更新了我在那里的progressBar1。
我的问题是如何在fileDownloadRadar中实时计算下载进度以及如何将其报告给splash.UpdateProgressBar方法?
答案 0 :(得分:1)
在我看来,你应该在单独的线程中运行文件下载,然后使用事件来更新进度条。
您可以通过计算左字节数来检查进度。
以下是包含您的问题解决方案的simillar问题: C# download file with progress indicator and then install file