当前位置: 首页 > 图文教程 > 网络编程 > ASP > ASP实现文件直接下载

ASP
asp中常用的文件处理函数
正确处理ASP动态网页中的容错机制
asp自动生成javascript检验函数
如何避免asp的SQL的执行效率低
编译asp应用程序成为exe文件
ASP完成小偷程序机制和简略示例
ASP技巧:rs.getrows方法
ASP简单实现数字字符混合验证码
ASP技巧 挂QQ的网页源代码ASP/PHP
ASP实例:用ASP判断文件地址是否有效
ASP实例:使用ASP生成图片彩色校验码
ASP如何跳出本次进入下一次循环
ASP读取数据库中数据到数组的类
为你的ASP程序作一个负载测试
如何用ASP来获取客户端真实IP的地址
asp脚本运行超时的解决办法
ASP缓存类 【先锋缓存类】Ver2004
ASP动态包含文件的改进方法
ASP中利用application实现缓存
ASP Application 对象用户手册

ASP实现文件直接下载


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 126 ::
收藏到网摘: n/a

在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
%>