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

ASP
asp下查询xml的实现代码
一个asp版XMLDOM操作类
ASP XML编程objXML.async = False
asp按关键字查询XML的代码
asp下用fso和ado.stream写xml文件的方法
SQL"不能为新插入的行确定标识"错误的解决方法
asp 类型转换函数大全
ASP存储过程开发应用详解
asp 静态页面的另一种思路
EasyASP v1.5发布(包含数据库操作类,原clsDbCtrl.asp)
ASP访问数量统计代码
asp中文数字验证码
Ajax+asp应用实例 注册模块,表单提交
将ASP记录集输出成n列的表格形式显示的方法
ASP 游标参数详解(ASP记录集)
关于ASP循环表格的问题之解答[比较详细]
ASP Recordset 分页显示数据的方法(修正版)
ASP ajax分页教程一
Asp Oracle存储过程返回结果集的代码
XMLHttp ASP远程获取网页内容代码

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


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