如何以百分比计算文件下载进度?
How can i calculate the progress of a file download in percentages?
我有这个方法:
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。
在表格 splash 中,我更新了那里的 progressBar1。
我的问题是如何在fileDownloadRadar中实时计算下载进度以及如何将其报告给splash.UpdateProgressBar方法?
我认为您应该运行在单独的线程中下载文件,然后使用事件更新进度条。
您可以通过计算剩余字节数来查看进度。
这是包含您的问题解决方案的类似问题:
C# download file with progress indicator and then install file
我有这个方法:
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。 在表格 splash 中,我更新了那里的 progressBar1。
我的问题是如何在fileDownloadRadar中实时计算下载进度以及如何将其报告给splash.UpdateProgressBar方法?
我认为您应该运行在单独的线程中下载文件,然后使用事件更新进度条。
您可以通过计算剩余字节数来查看进度。
这是包含您的问题解决方案的类似问题: C# download file with progress indicator and then install file