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

利用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();}

转载请注明:代码学堂>编程开发 > ASP.NET > 在.NET中利用XMLHTTP下载文件的代码

1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!如有侵权请邮件联系客服!10210454@qq.com
2. 本站不保证所提供所有下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理,有奖励!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!
4. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有RB奖励和额外RMB收入!

磊宇堂正在使用的服务器 维护管理由磊宇云服务器提供支持

磊宇堂 » 在.NET中利用XMLHTTP下载文件的代码

提供最优质的资源集合

立即查看 了解详情