.net中xmlhttp下载文件的方法参考

1@ Page language="c#"

< %@import namespace=MSXML2%>

 1<script language="c#" runat="server">   
 2private void Page_Load(object sender, System.EventArgs e)   
 3{   
 4string Url = " http://www.cccar.com.cn/images/index2_01.gif ";   
 5string StringFileName = Url.Substring(Url.LastIndexOf("/") + 1);   
 6string StringFilePath = Request.PhysicalApplicationPath;   
 7if(!StringFilePath.EndsWith("/")) StringFilePath += "/";   
 8MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();   
 9_xmlhttp.open("GET",Url,false,null,null);   
10_xmlhttp.send("");   
11if( _xmlhttp.readyState == 4 )   
12{   
13if(System.IO.File.Exists(StringFilePath + StringFileName))   
14System.IO.File.Delete(StringFilePath + StringFileName);   
15System.IO.FileStream fs = new System.IO.FileStream(StringFilePath + StringFileName, System.IO.FileMode.CreateNew);   
16System.IO.BinaryWriter w = new System.IO.BinaryWriter(fs);   
17w.Write((byte[])_xmlhttp.responseBody);   
18w.Close();   
19fs.Close();   
20Response.Write ("文件已经得到。<br><a href='" + Request.ApplicationPath + "\\\" + StringFileName +"' target='_blank'>");   
21Response.Write ("查看" + StringFileName + "</a>");   
22}   
23else   
24//Response.Write (_xmlhttp.statusText);   
25Response.End();   
26}   
27</script>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus