随笔-13  评论-5  文章-3  trackbacks-0

来源:http://www.caspnet.com.cn/blog/post/111.html

下面为完整的源代码

以下是引用片段:
<%@ Page language="c#"%> 
<%@import namespace=MSXML2%> 
<script language="c#" runat="server"> 
private void Page_Load(object sender, System.EventArgs e) 

string Url = "/blog/upload/20056222116262.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(); 

</script> 


 

 

//试分析 .NET 下用MSXML远程下载文件跟处理方法

//声明为 C#语言
<%@ Page language="c#"%>

//导入MSXML2空间,表示下面要用到这个空间里的一些内容
<%@import namespace=MSXML2%>
//语言C# 运行在服务器端
<script language="c#" runat="server">

private void Page_Load(object sender, System.EventArgs e)
{

//定义变量  Url 为一个绝对目录地址
string Url = "/blog/upload/20056222116262.gif";

//定义变量 取得以 / 为分隔符的 最后那个数组数据  得到 20056222116262.gif
string StringFileName = Url.Substring(Url.LastIndexOf("/") + 1);

//定义变量,得到当前文件的物理路径
string StringFilePath = Request.PhysicalApplicationPath;

//如果路径最后的字符不为 / 则加上 /
if(!StringFilePath.EndsWith("/")) StringFilePath += "/";

//申请 XMLHTTP对象

MSXML2.XMLHTTP _xmlhttp = new MSXML2.XMLHTTPClass();

// 用 GET方式 向URL地址 需要等待服务器回应 False
_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);

//写入到文件中。。内容为 XMLHTTP抓取的数据。
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();
}
</script>

posted on 2005-11-17 21:24 登陆不起啦 阅读(493) 评论(0)  编辑 收藏 引用 所属分类: 服务器端脚本
只有注册用户登录后才能发表评论。