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

ASP
ASP实现表单中容量大的数据的提交方法
强烈推荐ASP初学者观看:常用ASP技术
认真学习ASP内置对象Session的应用
asp函数转换xml中的实体字符[转义符]
ASP防止重复多次提交表单的方法
ASP处理XSLT转换XML的实现
ASP中将Excel数据导入到Access
[推荐]ASP 编程中 20 个非常有用的例子
[推荐]每个ASP程序员必备的知识
[推荐]ASP初学者常犯的几个错误
解决杀毒软件误删asp文件的方法
用VB编写ActiveX DLL实现ASP编程
asp.net结合js的网页打印程序
ASP网站远程客户实现EXCEL打印功能
ASP程序中使用断开的数据记录集
利用ASP发送和接收XML数据的处理方法
20种看ASP程序源码的方法及工具
ASP技巧:禁用FileSystemObject组件
在ASP中使用SQL语句:开始执行
ASP编写计数器的优化方法

ASP实现文件直接下载


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 128 ::
收藏到网摘: 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
%>