在.NET中利用XMLHTTP下载文件的代码
程序员文章站
2023-11-27 12:29:28
利用xmlhttp下载文件,和以前的方法一样,先添加引用-com-microsoft xml 3.0,然后在代码开始处写: using msx...
利用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();}
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中读取XML文件信息的4种方法与示例代码
下一篇: Python玩转PDF的各种骚操作