当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 在.NET中利用XMLHTTP下载文件的代码

ASP.NET
ewebeditor在.net的使用方法
Server.Transfer,Response.Redirect的区别
ASP.NET2.0+SQL Server2005构建多层应用
ASP.NET 2.0 中收集的小功能点(转)
ASP.Net全局变量的设置和读取方法
数据库开发总结(ADO.NET小结)
ASP.net(c#)打造24小时天气预报及实时天气
发布WEB站点时出现Server Application Unavailable
在asp.net中实现datagrid checkbox 全选的方法
ASP.NET 2.0 URL映射技巧
ConfiguraionSource节点及多个配置文件的应用
SqlConnection.ConnectionString相关关键字
如何在WebForm中使用javascript防止连打(双击)
看到本质而不是现象--解决ASP.NET CS0016的问题
学会区分Visual Studio 2005,Visual Studio 2005 Team System和MSDN Premium 订阅的各个版本
ASP.NET 入门的五个步骤
ASP.NET 高性能分页代码
动态ItemTemplate的实现(译) - item,template
遍历Hashtable 的几种方法
通过VS中的数据源选择对话框简单实现数据库连接配置

ASP.NET 中的 在.NET中利用XMLHTTP下载文件的代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 41 ::
收藏到网摘: n/a

利用XMLHTTP下载文件,和以前的方法一样,先添加引用-COM-Microsoft Xml 3.0,然后在代码开始处写:
using MSXML2;
下面就是主要的代码:
private void Page_Load(object sender, System.EventArgs e){
string Url = "http://dotnet.aspx.cc/Images/logoSite.gif";
string StringFileName = Url.Substring(Url.LastIndexOf("/") + 1);
string StringFilePath = Request.PhysicalApplicationPath;
if(!StringFilePath.EndsWith("/"))
StringFilePath += "/";
MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();
_xmlhttp.open("GET",Url,false,null,null);
_xmlhttp.send("");
if( _xmlhttp.readyState == 4 ) {
if(System.IO.File.Exists(StringFilePath + StringFileName))
System.IO.File.Delete(StringFilePath + StringFileName);
System.IO.FileStream fs = new System.IO.FileStream(StringFilePath + StringFileName, System.IO.FileMode.CreateNew);
System.IO.BinaryWriter w = new System.IO.BinaryWriter(fs);
w.Write((byte[])_xmlhttp.responseBody);
w.Close();
fs.Close();
Response.Write ("文件已经得到。<br><a href='" + Request.ApplicationPath + StringFileName +"' target='_blank'>");
Response.Write ("查看" + StringFileName + "</a>");
}
else
Response.Write (_xmlhttp.statusText); Response.End();}