在.NET中利用XMLHTTP下載文件的代碼
更新時(shí)間:2007年03月16日 00:00:00 作者:
利用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 ("文件已經(jīng)得到。<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 ("文件已經(jīng)得到。<br><a href='" + Request.ApplicationPath + StringFileName +"' target='_blank'>");
Response.Write ("查看" + StringFileName + "</a>");
}
else
Response.Write (_xmlhttp.statusText); Response.End();}
相關(guān)文章
ASP.NET連接MySql數(shù)據(jù)庫的2個(gè)方法及示例
這篇文章主要介紹了ASP.NET連接MySql數(shù)據(jù)庫的2個(gè)方法及示例,使用的是MySQL官方組件和ODBC.NET,需要的朋友可以參考下2014-03-03asp.net錯(cuò)誤捕獲(錯(cuò)誤處理)page_error事件使用方法
Page_Error事件提供了一種捕獲頁面級(jí)錯(cuò)誤的方法。對于錯(cuò)誤的處理,您可以只是顯示錯(cuò)誤信息(正如下面的示例代碼所示),也可以記錄事件或執(zhí)行某個(gè)其他操作2014-01-01asp.net 讀取xml文件里面的內(nèi)容,綁定到dropdownlist中
asp.net 讀取xml文件里面的內(nèi)容,綁定到dropdownlist中的實(shí)現(xiàn)代碼。2009-05-05Repeater控件動(dòng)態(tài)變更列(Header,Item和Foot)信息(重構(gòu)cs)
上一篇雖然它算不上是完全動(dòng)態(tài)化,但它已經(jīng)達(dá)到初期想要的效果,現(xiàn)另開一篇,不是重新另外寫,而是想重構(gòu)cs的代碼,因?yàn)榍耙黄拇a雖然簡單,但代碼冗余過多,感興趣的朋友可以參考下哈2013-03-03NetCore實(shí)現(xiàn)全局模型綁定異常信息統(tǒng)一處理(場景分析)
本文主要講解NetCore如何使用中間件捕獲模型綁定的異常信息,對NetCore實(shí)現(xiàn)全局模型綁定異常信息統(tǒng)一處理場景分析及實(shí)現(xiàn)代碼感興趣的朋友一起看看吧2021-12-12C#調(diào)用動(dòng)態(tài)unlha32.dll解壓Lha后綴的打包文件分享
這篇文章介紹了,C#調(diào)用動(dòng)態(tài)unlha32.dll解壓Lha后綴的打包文件,有需要的朋友可以參考一下2013-09-09