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

ASP.NET
asp.net(C#) 生成随机验证码的代码
C# 生成高质量缩略图程序—终极算法
iis的http 500内部服务器错误的解决
效控制C#中label输出文字的长度,自动换行
asp.net下生成99个不同的随机数
Asp.Net文本换行
Asp.Net中文本换行
asp.net 上传大文件解决方案
asp.net程序编译调试时偶尔出现访问被拒绝的错误的解决方法
控件开发时两种JS嵌入资源方式的使用方法
AspNetPager分页控件源代码(Version 4.2)
asp. net下使用foreach简化文本文件的访问。
asp.net下经典数据库记录分页代码
微软发布的Data Access Application Block的使用代码
如何在网站级别动态更改主题
asp.net下模态对话框关闭之后继续执行服务器端代码的问题
asp.net下利用JS实现对后台CS代码的调用方法
批量删除记录时如何实现全选方法总结
asp.net下OnClientClick的妙用!
[c#]asp.ent下开发中Tag的开发技巧

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 45 ::
收藏到网摘: 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();}