当前位置: 移动技术网 > IT编程>开发语言>.net > 在.NET中利用XMLHTTP下载文件的代码

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

2018年04月28日  | 移动技术网IT编程  | 我要评论

电影苹果被删片段,叶丁瑜,媚拉

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网