首页 ┆ 网站地图 ┆ 在线留言 ┆ 游戏资讯 ┆ 资源下载 
设为首页
加入收藏
联系我们
热门关键字: .net应用  操作系统  Dreamweaver  WinRAR  网络推广
高级搜索
您当前的位置: 主页>ASP专区>编程技巧>ASP单击链接直接下载文件的代码
ASP单击链接直接下载文件的代码
来源: 发布时间:2008-04-21 发布人: 浏览: 人次   字体: [ ]  
在ie中点开图片或文档的链接里,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   
%
>  
<%@ 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
%
>

相 关 文 章   发布商链接
·如何用ASP调用带参数存储过程?
·如何清理asp站点缓存的代码?
·asp获取字符长度函数和截取字符串函...
·asp防止用户刷新多次提交表单和使用...
·利用windows计划任务实现定时运行ASP...
·asp读取数据库数据并生成excel文件实...
·ASP开发10条经验总结
·Asp及Web开发中的常见问题小总结
·破解网站发布系统,ASP生成静态页面方...
·将ASP纪录集输出成n列的的表格形式显...
 §最新评论:(评论内容只代表网友观点,与本站立场无关!)
网名: 验证码:  【所有评论】【↑返回顶部
评 分: 12 345
评论内容:(不能超过500字,请自觉遵守互联网相关政策法规。[按 Ctrl+Enter 可直接提交]
注意:请勿在本站发布政治话题、色情及违反法律的内容。
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。
推 荐 文 章
·asp读取数据库数据并生成exce...
·利用windows计划任务实现定时
·asp防止用户刷新多次提交表单
·asp获取字符长度函数和截取字
·如何清理asp站点缓存的代码?
·如何用ASP调用带参数存储过程
·ASP实现从远程服务器上接收XM...
·asp自动实时无刷新更新数据
·如何防止网页的内容被别人采...
·ASP实现自动解压RAR文件的代...
·破解网站发布系统,ASP生成静...
热 门 文 章
·Asp及Web开发中的常见问题小...
·破解网站发布系统,ASP生成静...
·asp自动实时无刷新更新数据
·如何防止网页的内容被别人采...
·ASP开发10条经验总结
·如何清理asp站点缓存的代码?
·asp获取字符长度函数和截取字...
·ASP实现自动解压RAR文件的代...
·ASP实现从远程服务器上接收XM...
·将ASP纪录集输出成n列的的表...
·如何用ASP调用带参数存储过程...
·利用windows计划任务实现定时...
·asp读取数据库数据并生成exce...
·asp防止用户刷新多次提交表单...
网站首页 - 关于本站 - 加入收藏 - 网站地图 - 友情连接 - 在线留言 - 联系我们 - 返回顶部
Copyright © 2007 IT知道网.[冀ICP备07026896号]. All Rights Reserved .