您的位置:首页-> 技术开发-> ASP学院-> ASP实现文件直接下载

ASP实现文件直接下载
作者:不详 时间:2005-8-25  来自:ASP教程  选择阅读文体:


在IE进行文档链接时,如果遇到OLE支持的文档,IE会自动调用相应程序打开它,有时候这种功能并不是我们所需的,虽然我们可以提醒用户用鼠标右键-->"目标另存为...."命令来下载文档,但这样毕竟不太友好,本文描述了利用FSO及Stream方法实现IE直接下载文档.

<%@ language=vbscript codepage=65001%>

<%
'Filename must be input
if Request("Filename")="" then
 response.write "<h1>Error:</h1>Filename is empty!<p>"
else
call  downloadFile(replace(replace(Request("Filename"),"\",""),"/",""))   
 
Function  downloadFile(strFile)   
'  make  sure  you  are  on  the  latest  MDAC  version  for  this  to  work   
'  get  full  path  of  specified  file   
strFilename  =  server.MapPath(strFile)   
 
'  clear  the  buffer   
Response.Buffer  =  True   
Response.Clear   
 
'  create  stream   
Set  s  =  Server.CreateObject("ADODB.Stream")   
s.Open   
 
'  Set  as  binary   
s.Type  =  1   
 
'  load  in  the  file   
on  error  resume  next   
 
'  check  the  file  exists
Set  fso  =  Server.CreateObject("Scripting.FileSystemObject")   
if  not  fso.FileExists(strFilename)  then   
Response.Write("<h1>Error:</h1>"&strFilename&" does not exists!<p>")   
Response.End   
end  if
 
'  get  length  of  file   
Set  f  =  fso.GetFile(strFilename)   
intFilelength  =  f.size   
 
s.LoadFromFile(strFilename)   
if  err  then   
Response.Write("<h1>Error: </h1>Unknown Error!<p>")   
Response.End
end  if 

'  send  the  headers  to  the  users  Browse
Response.AddHeader  "Content-Disposition","attachment;  filename="&f.name   
Response.AddHeader  "Content-Length",intFilelength   
Response.CharSet  =  "UTF-8"   
Response.ContentType  =  "application/octet-stream"   

'  output  the  file  to  the  browser   
Response.BinaryWrite  s.Read   
Response.Flush   

'  tidy  up   
s.Close   
Set  s  =  Nothing   

End  Function   
end if
%>     




上一篇:WEB打印设置解决方案一(通过修改注册表改变IE打印设置) 下一篇:XMLHTTP Get HTML页面时的中文乱码之完全客户端Script解决方案

GoogLe中搜索“ASP实现文件直接下载”相关内容...
百 度 中搜索“ASP实现文件直接下载”相关内容...
一 搜 中搜索“ASP实现文件直接下载”相关内容...

相关文章

.Net专栏
Script
ASP学院
PHP学院
Web服务器
CGI学院
XML学院
SQL学院
JSP学院
SMS学院
提升QQ等级 QQ挂机升级外挂
国内首发 韩国版QQ自定义头像
挖掘Windows XP系统安装盘中的神秘宝藏
初见photoshop8.0的庐山真面目!
BT下载速度之战:Bitcomet对决比特精灵
腾讯最新漏洞-------QQ红钻任你拿
Windows XP 的安装
如何制作万能DVD
浅谈Windows XP SP2补丁包的安装及使用
RM,RMVB转VCD
ASP 3.0高级编程(一)
无组件文件上传源代码
ASP 3.0高级编程(二)
ASP,PHP,JS 版本完全挂Q程序源代码
ASP连接数据库的全能代码
用ASPJPEG组件制作图片的缩略图和加水印
几个典型的ASP应用
ASP小偷程序如何利用XMLHTTP实现表单的提交以及cookies或session的发送
常见ASP组件介绍及下载
一个18位身份证校验计算函数