查看MSND上对webclient的解释,有上传与下载方法:
WebClient提供一下上传方法:
WebClient提供一下下载方法:
WebClient和HttpWebRequst是用来获取数据的2种方式,一般而言,WebClient更倾向于“按需下载”,事实上掌握它也是相对容易的,而HttpWebRequst则允许你设置请求头或者对内容需要更多的控制,
后者有点类似于form中的submit。虽然两者都是异步请求事件,但是WebClient是基于事件的异步,而HttpWebRequst是基于代理的异步编程。
文件下载:
protected void Button1_Click(object sender, EventArgs e)
{
String url = "http://localhost/SprayerWeb/download/1.zip";
WebClient webclient = new WebClient();
byte [] bytes = webclient.DownloadData(url);
string path = string.Format("{0}\\{1}\\{2}{3}", Server.MapPath("~"), "download", DateTime.Now.ToString("yyyyHHmmHHmmssfff"), ".zip");
FileStream fs = new FileStream(path, FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
WebClient webclient = new WebClient();
String url = "http://localhost/SprayerWeb/download/1.zip";
Stream stream = webclient.OpenRead(url);
string path = string.Format("{0}\\{1}\\{2}{3}", Server.MapPath("~"), "download", DateTime.Now.ToString("yyyyHHmmHHmmssfff"), ".zip");
FileStream fs = new FileStream(path, FileMode.Create);
byte[] bytes = new byte[8192];
int read = 0;
while ((read = stream.Read(bytes, 0, bytes.Length)) > 0) {
fs.Write(bytes, 0, read);//这儿需要使用read,不能使用bytes.Length
}
fs.Close();
}
protected void Button3_Click(object sender, EventArgs e)
{
WebClient webclient = new WebClient();
String url = "http://localhost/SprayerWeb/download/1.zip";
string path = string.Format("{0}\\{1}\\{2}{3}", Server.MapPath("~"), "download", DateTime.Now.ToString("yyyyHHmmHHmmssfff"), ".zip");
webclient.DownloadFile(url, path);
}
protected void Button4_Click(object sender, EventArgs e)
{
String url = "http://localhost/SprayerWeb/download/1.zip";
Uri uri = new Uri(url, UriKind.Absolute);
WebClient webclient = new WebClient();
webclient.OpenReadAsync(uri);
webclient.OpenReadCompleted+=webclient_OpenReadCompleted;
}
private void webclient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
Stream stream = e.Result;
byte[] bytes = new byte[8192];
int read = 0;
string path = string.Format("{0}\\{1}\\{2}{3}", Server.MapPath("~"), "download", DateTime.Now.ToString("yyyyHHmmHHmmssfff"), ".zip");
FileStream fs = new FileStream(path, FileMode.Create);
while ((read = stream.Read(bytes, 0, bytes.Length)) > 0) {
fs.Write(bytes, 0, read);
}
fs.Close();
stream.Close();
}
protected void Button5_Click(object sender, EventArgs e)
{
//在IIS6下没有执行成功,以后再调试吧
WebClient webclient = new WebClient();
string targetpath = string.Format("{0}{1}{2}","http://localhost/WebApp/upload/",DateTime.Now.ToString("yyyyMMddHHmmssfff"),".zip");
string sourcepath = string.Format("{0}\\{1}\\{2}{3}", Server.MapPath("~"), "download", "1", ".zip");
byte[] responseArray = webclient.UploadFile(targetpath, "PUT", sourcepath);
}
protected void Button6_Click(object sender, EventArgs e)
{
//在IIS6下没有执行成功,以后再调试吧
string targetpath = string.Format("{0}{1}{2}", "http://localhost/WebApp/upload/", DateTime.Now.ToString("yyyyMMddHHmmssfff"), ".zip");
string sourcepath = string.Format("{0}\\{1}\\{2}{3}", Server.MapPath("~"), "download", "1", ".zip");
WebClient webclient = new WebClient();
FileStream fs = new FileStream(sourcepath, FileMode.Open, FileAccess.Read);
byte[] bt = new byte[fs.Length];
fs.Read(bt, 0, bt.Length);
byte[] responseArray = webclient.UploadData(targetpath, "PUT", bt);
fs.Close();
}
protected void Button7_Click(object sender, EventArgs e)
{
WebClient webclient = new WebClient();
string sourcepath = string.Format("{0}\\{1}\\{2}{3}", Server.MapPath("~"), "download", "1", ".zip");
byte[] responseArray= webclient.UploadFile("http://localhost/WebApp/upload.aspx", "POST", sourcepath);
Response.Write("result:"+System.Text.Encoding.UTF8.GetString(responseArray));
}
protected void Page_Load(object sender, EventArgs e)
{
//string uploadpath = string.Format("{0}\\{1}{}");
foreach (string f in Request.Files.AllKeys)
{
HttpPostedFile postfile = Request.Files[f];
string path = String.Format("{0}\\{1}\\{2}{3}",Server.MapPath("~"),"upload",DateTime.Now.Ticks.ToString(),".zip");
postfile.SaveAs(path);
}
Response.Write("Success");
}
更多详细内容参考:https://msdn.microsoft.com/zh-cn/library/system.net.webclient%28VS.80%29.aspx