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

ASP
asp OpenTextFile文本读取与写入实例代码
在ASP编程中nothing代表什么意思?
asp之让Session永不过期
ASP获取ACCESS数据库表名及结构的代码
推荐一篇不错的新手asp编程的基本法则
最简洁的asp多重查询的解决方案
ASP 判断是否有中文的代码
asp.net常用函数收藏
asp下几种常用排序算法
asp获取数据库中表名和字段名的代码
简单的asp采集代码教程
asp生成带有样式的word文件方法
实用301转向到另一域名相应页面的asp代码
asp伪继承初探_实例代码
asp代理采集的核心函数代码
js table排序类代码
ASP UTF-8页面乱码+GB2312转UTF-8 +生成UTF-8格式的文件(编码)
Asp生成RSS的类_给网站加上RSS
utf-8 网页不显示+utf-8网页乱码的通用解决方法
asp截取指定英汉混合字符串_支持中文

ASP实现文件直接下载的代码


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