C#中使用WebClient对象下载文件
来源:互联网 发布:没有更新到数据库 编辑:程序博客网 时间:2024/05/01 01:29
/// <summary>
/// 下载文件
/// </summary>
/// <param name="sourceFileUrl"></param>
/// <param name="savePath"></param>
private void ActionDownLoadFile(string sourceFileUrlAddress, string savePath)
{
string fileName = "";
byte[] mbyte = new byte[51200];
int allmybyte = mbyte.Length;
long nowFinish = 0;
int m = 0;
//写入到BYTE数组中,起缓冲作用
FileStream fstr = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write);
int lastSplitChar = sourceFileUrlAddress.LastIndexOf("/");
WebClient client = new WebClient();
Stream str = null;
long totalbytes = 0;
try
{
this.ShowActionText("链接下载文件...");
fileName = sourceFileUrlAddress.Substring(lastSplitChar + 1); //获得文件名
client.DownloadFile(sourceFileUrlAddress, fileName);
str = client.OpenRead(sourceFileUrlAddress);
totalbytes = Convert.ToInt64(client.ResponseHeaders["Content-Length"]);
while (nowFinish < totalbytes || m > 0)
{
m = str.Read(mbyte, 0, allmybyte);
fstr.Write(mbyte, 0, m);
this.ShowActionText("正在接收数据... " + string.Format("{0:f}%,完成:{1},总共:{2} 字节", (double)(nowFinish * 100) / totalbytes, nowFinish, totalbytes));
if (m == 0)
break;
nowFinish += m;
}
str.Close();
fstr.Close();
this.ShowActionText("下载完毕!");
}
catch (WebException ex)
{
throw new Exception(ex.Message);
}
}
调用如下: ActionDownLoadFile("http://www.abc123.com/logo.jpg","E:/logo.jpg");
- C#中使用WebClient对象下载文件
- C#下载文件:WebClient
- C# 文件下载(WebClient)
- C#使用WebClient下载文件到本地目录
- 使用WebClient实现文件下载
- 使用WebClient实现文件下载
- 使用WebClient实现文件下载
- c# WebClient.DownloadFile文件下载方法
- WebClient 类的使用和下载文件
- c# webclient下载网页
- WebClient下载文件代码!
- WebClient下载文件
- WebClient异步下载文件
- webclient下载文件
- C#开发学习笔记:利用WebClient下载文件
- C#利用WebClient 两种方式下载文件
- 使用WebClient实现下载
- 使用WebClient下载
- 08百度笔试题
- 〖编程擂台〗用C#去掉SQL语句中的注释
- postgres中自动增长列的设定
- Beginning.Microsoft.Visual.C.Sharp.2008.翻译(1.1.3-1.1.4)
- 经典非原创,网页常用Javascript
- C#中使用WebClient对象下载文件
- GTK+主循环(main loop)的工作原理
- 移动层
- c# 多线程
- 23232323
- MY->> 读书笔记.每日更新();
- 通往WinDbg的捷径(一)
- 最近我在用treeview+自定义SiteMapDataSource控件读取数据库数据做导航列。
- 最近我在用treeview+自定义SiteMapDataSource控件读取数据库数据做导航列。