WebClient类(下载、上传文件)

下载文件:

如果只想把文件保存到磁盘上,就应该调用DownloadFile()方法,这个方法有两参数:文件URL和保存所请求的数据的位置(路径和文件名):

WebClient client = new WebClient();

client.DownloadFile("http://www.reuters.com/","ReutersHomepage.htm");

 

应用程序需要处理从Web站点检索到的数据,为此,要使用OpenRead()方法,这个方法返回一个Stream引用。然后,就可以把数据从数据流中提取到内存中:

WebClient client = new WebClient();

Stream strm = client.OpenRead("http://www.reuters.com/");

 

上传文件:

UploadFile()方法用于把指定的文件上传到指定的位置,其中的文件名已经给出;而UploadData()方法用于把二进制数据上传至指定的URL,那些二进制数据是作为字节数组提供的(还有一个DownloadData()方法,用于从URL中检索字节数组):

WebClient client = new WebClient();

client.UploadFile("http://www.ourwebsite.com/NewFile.htm","C:\\WebSiteFiles\\NewFile.htm");

byte[] image;

client.UploadDate("http://www.ourwebsite.com/NewFile.jpg",image);

posted @ 2010-01-04 15:59  flora_asp.net  阅读(351)  评论(0)    收藏  举报