C#中使用WebClient对象下载文件
2008-10-13 18:08
579 查看
/// <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");
/// 下载文件
/// </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 从Web 地址下载文件到本地.
- C#使用WebClient下载文件到本地目录
- ASP.NET上传下载文件示例,使用webClient实现
- C# WinForm通过WebClient实现文件上传下载
- 谈谈C#文件监控对象FileSystemWatcher使用感受
- C#利用WebClient 两种方式下载文件
- [C#]使用WebClient上传文件并同时Post表单数据字段到服务端
- 使用ftp上传文件和下载文件---C#
- C# WinForm通过WebClient实现文件上传下载 (附源码) 分类: C#.NET
- ASP.NET中使用C#文件下载四方法
- 转载:C# 文件下载(WebClient)
- 下载文件使用缓存(一次性读取到内存),优化性能(注意静态对象修改需要加锁)
- 谈谈C#文件监控对象FileSystemWatcher使用感受
- C#利用WebClient 两种方式下载文件
- 在C#使用文件监控对象FileSystemWatcher 实现数据同步
- NET(C#):使用HttpWebRequest头中的Range下载文件片段
- 下载软件后使用c#获取文件的md5码示例
- C#利用WebClient 两种方式下载文件
- c# WebClient文件下载
- 使用C#下载网络文件