c# - WebClient 不报告 0Kb 文件
问题描述
我正在编写一个从我们的服务器下载 .xml 文件并从中检索一些数据的应用程序。
该文件是我们在我部门使用的零件的数据。
有些文件是 0Kb,这意味着我们没有这部分的数据。
现在我的问题是完成下载 0Kb 文件时 Webclient DownloadFileCompleted 事件没有触发。
这是我的代码:
private void Get_Tool_Model()
{
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
download_Tool_Model = true;
using (WebClient wc = new WebClient())
{
wc.UseDefaultCredentials = true;
wc.DownloadProgressChanged += (o, e) =>
{
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn / totalBytes * 100;
Dispatcher.Invoke(() => {
txt_progress.Text = "Downloaded " + e.BytesReceived + " of " + e.TotalBytesToReceive;
progress.Value = e.ProgressPercentage;
});
};
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(Wc_DownloadFileCompleted);
wc.DownloadFileAsync(
// Param1 = Link of file
new System.Uri(path),
// Param2 = Path to save
"C:\\Tool_Model.xml"
);
}
}
}
private void Wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
download_completed = true;
}
谢谢您的帮助。
解决方案
推荐阅读
- html - 我正在尝试抓取 Moderna/pfizer 网站,以查看他们发布新闻的日期。R中的代码给出输出字符(0)而不是日期
- html - 当我放大和缩小我的网页(html和css)时,div/文本移动
- javascript - API 查询(curl 与 axios)
- javascript - 嵌套函数参数从何而来?
- java - JSP中实现推特的问题:IF语句不能正常工作
- postgresql - 减去一天Postgresql
- python - 如何使用最大似然估计将负二项模型拟合到数据中
- java - 登录认证后spring boot jwt没有重定向
- python - 如何在不更改代码的情况下从任意位置导入 Python 文件?
- c++ - 当客户端数据的步幅较小时上传到统一缓冲区对象