Web下载文件
IE浏览器在打开swf、wma等文件的时候会自动打开,如果想下载到本地存储就不太方便,为此我自己做一个小程序用来下载文件,
由于我在局域网内使用代理服务器上网,所以下载文件还需要通过代理设置。
程序截图:
下载按钮事件的代码:
private void buttonDownload_Click(object sender, EventArgs e)
{
TIMMS.DATA.TWebClient wc = new TIMMS.DATA.TWebClient();
if (checkBox1.Checked)
{
wc.ProxyUserName = textBoxUsername.Text;
wc.ProxyPassword = textBoxPassword.Text;
wc.ProxyDomainName = textBoxDomainname.Text;
}
wc.ProxyUrl = textBoxUrl.Text;
SaveFileDialog saveFileDialog = new SaveFileDialog();
int indexof=textBoxUrl.Text.LastIndexOf("/")+1;
int length=textBoxUrl.Text.Length;
string filename=textBoxUrl.Text.Substring(indexof, (length - indexof));
string file_extension = System.IO.Path.GetExtension(filename);
saveFileDialog.FileName = filename;
saveFileDialog.Filter = "文件 (*" + file_extension + ")|*" + file_extension + "|所有文件|*.*";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
wc.FileName = saveFileDialog.FileName;
}
string errmsg="";
Cursor.Current = Cursors.WaitCursor;
buttonDownload.Enabled = false;
if (!wc.GetFile(out errmsg))
{
MessageBox.Show(errmsg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
buttonDownload.Enabled = true;
Cursor.Current = Cursors.Default;
return;
}
buttonDownload.Enabled = true;
Cursor.Current = Cursors.Default;
MessageBox.Show("下载完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information );
}
TIMMS.DATA.TWebClient :
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
namespace TIMMS.DATA
{
public class TWebClient
{
/// <summary>
/// 供向 URI 标识的资源发送数据和从 URI 标识的资源接收数据的公共方法
/// </summary>
public TWebClient() { }
/// <summary>
/// TWebClient构造函数
/// </summary>
/// <param name="user">代理服务器用户名</param>
/// <param name="password">代理服务器登录密码</param>
/// <param name="domainName">代理服务器域名</param>
/// <param name="Address">代理服务器地址</param>
/// <param name="url">需要下载的Web文件路径</param>
/// <param name="filename">保存到本地的文件名</param>
public TWebClient(string user,string password,string domainName,string Address,string url,string filename)
{
proxyUser=user;
ProxyPassword=password;
proxyDomainName=domainName;
proxyAddress = Address;
proxyUrl = url;
fileName = filename;
}
/// <summary>
/// TWebClient构造函数,使用IE默认代理设置
/// </summary>
/// <param name="user">代理服务器用户名</param>
/// <param name="password">代理服务器登录密码</param>
/// <param name="domainName">代理服务器域名</param>
/// <param name="url">需要下载的Web文件路径</param>
/// <param name="filename">保存到本地的文件名</param>
public TWebClient(string user, string password, string domainName, string url, string filename)
{
proxyUser = user;
ProxyPassword = password;
proxyDomainName = domainName;
proxyUrl = url;
fileName = filename;
}
/// <summary>
/// WebClient构造函数,使用IE默认代理设置
/// </summary>
/// <param name="user">代理服务器用户名</param>
/// <param name="password">代理服务器登录密码</param>
/// <param name="domainName">代理服务器域名</param>
public TWebClient(string user, string password, string domainName)
{
proxyUser = user;
ProxyPassword = password;
proxyDomainName = domainName;
}
/// <summary>
/// TWebClient构造函数
/// </summary>
/// <param name="url">需要下载的Web文件路径</param>
/// <param name="filename">保存到本地的文件名</param>
public TWebClient(string url, string filename)
{
proxyUrl = url;
fileName = filename;
}
private string proxyUser = "";
/// <summary>
/// 代理服务器用户名
/// </summary>
public string ProxyUserName
{
get { return proxyUser; }
set { proxyUser = value; }
}
private string proxyPassword = "";
/// <summary>
/// 代理服务器用户密码
/// </summary>
public string ProxyPassword
{
get { return proxyPassword; }
set { proxyPassword = value; }
}
private string proxyDomainName = "";
/// <summary>
/// 代理服务器域名
/// </summary>
public string ProxyDomainName
{
get { return proxyDomainName; }
set { proxyDomainName = value; }
}
private string proxyAddress;
/// <summary>
/// 代理服务器地址http://172.16.102.15:8080/,
/// 默认与IE设置相同
/// </summary>
public string ProxyAddress
{
get { return proxyAddress; }
set { proxyAddress = value; }
}
private string proxyUrl = "http://www.baidu.com/img/baidu_logo.gif";
/// <summary>
/// 代理服务器访问地址,默认:"http://www.baidu.com/img/baidu_logo.gif"
/// </summary>
public string ProxyUrl
{
get { return proxyUrl; }
set { proxyUrl = value; }
}
private string fileName = "";
/// <summary>
/// 下载文件的文件名(包含路径)@"d:\baidu_logo.gif";
/// </summary>
public string FileName
{
get { return fileName; }
set { fileName = value; }
}
/// <summary>
/// 根据TWebClient设置获取并保存文件
/// </summary>
/// <param name="errmsg"></param>
/// <returns></returns>
public bool GetFile(out string errmsg)
{
try
{
WebClient wc = new WebClient();
WebProxy proxy = WebProxy.GetDefaultProxy();//获取IE缺省设置
//如果缺省设置为空,则有可能是根本不需要代理服务器,如果此时配置文件中也未配置则认为不需Proxy
if (proxy.Address == null && proxyAddress != null && proxyAddress != "")
proxy.Address = new Uri(proxyAddress);//按配置文件创建Proxy 地置
if (proxy.Address != null)//如果地址为空,则不需要代理服务器
{
proxy.Credentials = new NetworkCredential(proxyUser, proxyPassword, proxyDomainName);//从配置封装参数中创建
wc.Proxy = proxy;//request.Proxy = proxy;//赋予 request.Proxy
}
wc.DownloadFile(proxyUrl, fileName);//下载文件并保存
errmsg = "OK";
return true;
}
catch (Exception ex)
{
errmsg = ex.Message;
return false;
}
}
/// <summary>
/// 根据TWebClient设置获取并保存文件
/// </summary>
/// <param name="url">需要下载的Web文件路径</param>
/// <param name="filename">保存到本地的文件名</param>
/// <param name="errmsg">错误消息</param>
/// <returns>成功true/失败false</returns>
public bool GetFile(string url, string filename,out string errmsg)
{
proxyUrl = url;
fileName = filename;
return GetFile(out errmsg);
}
}
}