ASP实现文件直接下载

来源:wdchn.com 时间:2008-11-19 15:39:00 

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

标签:
0
投稿

猜你喜欢

  • Django migrate报错的解决方案

    2021-05-16 12:48:30
  • 影响SEO的页面制作细节

    2008-10-18 16:06:00
  • Python使用list列表和tuple元组的方法

    2022-08-10 22:01:49
  • MySQL数据库同时查询更新同一张表的方法

    2024-01-22 23:10:38
  • Python中如何添加自定义模块

    2023-06-09 23:24:38
  • python采用django框架实现支付宝即时到帐接口

    2023-07-16 00:15:33
  • 好用的asp防SQL注入代码

    2008-10-24 08:36:00
  • Python while 循环使用的简单实例

    2022-11-16 15:56:18
  • 详解Python中的静态方法与类成员方法

    2022-08-02 23:30:16
  • Golang使用Gin框架实现HTTP上传文件过程介绍

    2024-04-28 09:18:12
  • Microsoft VBScript 运行时错误 错误800a0005 无效的过程调用或参数

    2010-03-25 21:51:00
  • MySQL死锁问题分析及解决方法实例详解

    2024-01-23 12:40:18
  • python排序算法之希尔排序

    2023-03-03 13:50:48
  • Python使用Tkinter实现机器人走迷宫

    2022-12-18 16:49:51
  • nodejs开发——express路由与中间件

    2024-05-11 10:18:04
  • Java采用setAsciiStream方法检索数据库指定内容实例解析

    2024-01-28 18:40:33
  • Python自动录入ERP系统数据

    2022-03-09 06:05:41
  • Go Map并发冲突预防与解决

    2024-02-19 00:25:24
  • 浅析SQL Server中包含事务的存储过程

    2024-01-15 21:27:39
  • 基于Python实现的ID3决策树功能示例

    2021-10-24 07:57:24
  • asp之家 网络编程 m.aspxhome.com