利用XMLHTTP下载文件,和以前的方法一样,先添加引用-COM-Microsoft Xml 3.0,然后在代码开始处写:
<br />using MSXML2;<br /> 下面就是主要的代码: </td><br /> </tr><br /> </tbody><br /></table><br /><div style="border: 0.5pt solid windowtext; padding: 4px 5.4pt; background: rgb(230, 230, 230) none repeat scroll 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; width: 95%;"><br /><div><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/None.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/None.gif" alt="" /><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;Page_Load(</span><span style="color: rgb(0, 0, 255);">object</span><span style="color: rgb(0, 0, 0);">&nbsp;sender,&nbsp;System.EventArgs&nbsp;e)<br /><img align="top" onclick="this.style.display='none'; document.getElementById('_58_1137_Open_Text').style.display='none'; document.getElementById('_58_1137_Closed_Image').style.display='inline'; document.getElementById('_58_1137_Closed_Text').style.display='inline';" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" id="_58_1137_Open_Image" alt="" /><img align="top" onclick="this.style.display='none'; document.getElementById('_58_1137_Closed_Text').style.display='none'; document.getElementById('_58_1137_Open_Image').style.display='inline'; document.getElementById('_58_1137_Open_Text').style.display='inline';" style="display: none;" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" id="_58_1137_Closed_Image" alt="" /></span><span style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255); display: none;" id="_58_1137_Closed_Text">...</span><span id="_58_1137_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">&nbsp;Url&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">http://dotnet.aspx.cc/Images/logoSite.gif</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">;<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">&nbsp;StringFileName&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;Url.Substring(Url.LastIndexOf(</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">)&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">获取当前正在执行的服务器应用程序的根目录的物理文件系统路径</span><span style="color: rgb(0, 128, 0);"><br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">&nbsp;StringFilePath&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;Request.PhysicalApplicationPath;<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">!</span><span style="color: rgb(0, 0, 0);">StringFilePath.EndsWith(</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">))&nbsp;StringFilePath&nbsp;</span><span style="color: rgb(0, 0, 0);">+=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">;<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;MSXML2.XMLHTTP&nbsp;_xmlhttp&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;MSXML2.XMLHTTPClass();<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;_xmlhttp.open(</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">GET</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">,Url,</span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">);<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;_xmlhttp.send(</span><span style="color: rgb(0, 0, 0);">&quot;&quot;</span><span style="color: rgb(0, 0, 0);">);<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" /></span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(&nbsp;_xmlhttp.readyState&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">4</span><span style="color: rgb(0, 0, 0);">&nbsp;)<br /><img align="top" onclick="this.style.display='none'; document.getElementById('_483_1072_Open_Text').style.display='none'; document.getElementById('_483_1072_Closed_Image').style.display='inline'; document.getElementById('_483_1072_Closed_Text').style.display='inline';" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" id="_483_1072_Open_Image" alt="" /><img align="top" onclick="this.style.display='none'; document.getElementById('_483_1072_Closed_Text').style.display='none'; document.getElementById('_483_1072_Open_Image').style.display='inline'; document.getElementById('_483_1072_Open_Text').style.display='inline';" style="display: none;" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" id="_483_1072_Closed_Image" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border: 1px solid rgb(128, 128, 128); background-color: rgb(255, 255, 255); display: none;" id="_483_1072_Closed_Text">...</span><span id="_483_1072_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(System.IO.File.Exists(StringFilePath&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;StringFileName))<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.IO.File.Delete(StringFilePath&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;StringFileName);<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.IO.FileStream&nbsp;fs&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;System.IO.FileStream(StringFilePath&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;StringFileName,&nbsp;System.IO.FileMode.CreateNew);<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">Write&nbsp;The&nbsp;_xmlhttp.responseBody&nbsp;into&nbsp;Local&nbsp;Stream&nbsp;fs&nbsp;</span><span style="color: rgb(0, 128, 0);"><br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.IO.BinaryWriter&nbsp;w&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;System.IO.BinaryWriter(fs);<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;w.Write((</span><span style="color: rgb(0, 0, 255);">byte</span><span style="color: rgb(0, 0, 0);">[])_xmlhttp.responseBody);<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;w.Close();<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fs.Close();<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Write&nbsp;(</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">文件已经得到。&lt;br&gt;&lt;a&nbsp;href='</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;Request.ApplicationPath&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;StringFileName&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">'&nbsp;target='_blank'&gt;</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">);<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Write&nbsp;(</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">查看</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;StringFileName&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">&lt;/a&gt;</span><span style="color: rgb(0, 0, 0);">&quot;</span><span style="color: rgb(0, 0, 0);">);<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);"><br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Write&nbsp;(_xmlhttp.statusText);<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;Response.End();<br /><img align="top" src="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" _fcksavedurl="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" alt="" />}</span></span></div><br /></div> |