特色栏目

ASP源码

PHP源码

.NET源码

JSP源码

游戏频道
专题合集
关闭菜单
首页> ASP教程> ASP实现文件直接下载

ASP实现文件直接下载

时间:2009-06-24 17:27:42 作者:互联网

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

<%@ language=vbscript codepage=65001%>

<%
'Filename must be input
if Request("Filename")="" then
 re***nse.write "

Error:

Filename is empty!

"
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  =  se***r.MapPath(strFile)   
 
'  clear  the  buffer   
Re***nse.Buffer ; =  True   
Re***nse.Clear ;  
 
'  create  stream   
Set  s  =  Se***r.CreateObject("AD***.Stream")   
s.***n ;  
 
'  Set  as  binary   
s.***e ; =  1   
 
'  load  in  the  file   
on  error  resume  next   
 
'  check  the  file  exists
Set  fso  =  Se***r.CreateObject("Sc***ting.FileSystemObject")   
if  not  fs***ileExists(strFilename)  then   
Re***nse.Write("

Error:

"&strFilename&" does not exists!

")   
Re***nse.End ;  
end  if
 
'  get  length  of  file   
Set  f  =  fs***etFile(strFilename)   
intFilelength  =  f.***e ;  
 
s.***dFromFile(strFilename)   
if  err  then   
Re***nse.Write("

Error:

Unknown Error!

")   
Re***nse.End
end  if 

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

'  output  the  file  to  the  browser   
Re***nse.BinaryWrite ; s.***d ;  
Re***nse.Flush ;  

'  tidy  up   
s.***se ;  
Set  s  =  Nothing   

End  Function   
end if
%>     

相关文章 最新文章

相关应用

热门文章

猜你喜欢

返回顶部