最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > asp教程

asp开发:ASP实现文件直接下载

日期:10-15    来源:中国设计秀    作者:cnwebshow.com

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

<%@ language=vbscript codepage=65001%>Sz5中国设计秀

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

'  send  the  headers  to  the  users  BrowseSz5中国设计秀
Response.AddHeader  "Content-Disposition","attachment;  filename="&f.name    Sz5中国设计秀
Response.AddHeader  "Content-Length",intFilelength    Sz5中国设计秀
Response.CharSet  =  "UTF-8"    Sz5中国设计秀
Response.ContentType  =  "application/octet-stream"   Sz5中国设计秀

'  output  the  file  to  the  browser    Sz5中国设计秀
Response.BinaryWrite  s.Read    Sz5中国设计秀
Response.Flush   Sz5中国设计秀

'  tidy  up    Sz5中国设计秀
s.Close    Sz5中国设计秀
Set  s  =  Nothing   Sz5中国设计秀

End  Function    Sz5中国设计秀
end ifSz5中国设计秀
%>     Sz5中国设计秀

本文引用地址:/bc/article_46556.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明