-
asp.net教程之ASP实现文件直接下载
- 2017-06-11 20:12 来源:未知
在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
%>
<%@ 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
%>
最新更新
unittest的TestCase类提供的判断方法
python基础教程之unittest模块(单元测试)
python基础教程之Python的特点(优点和缺点
python基础教程之Python是什么,Python简介
python基础教程之编译型语言和解释型语言
python基础教程之编程语言是什么
python基础教程之Python assert断言函数及用法
python基础教程之Python 文件 truncate() 方法
python基础教程之Python 文件 write() 方法
python基础教程之Python 文件 writelines() 方法
基于UDP的服务器端和客户端
再谈UDP和TCP
在socket编程中使用域名
网络数据传输时的大小端问题
socket编程实现文件传输功能
如何优雅地断开TCP连接?
图解TCP四次握手断开连接
详细分析TCP数据的传输过程
图解TCP数据报结构以及三次握手(非常详
TCP协议的粘包问题(数据的无边界性)
sql语句大全之Microsoft SQL Server 2012安装说明
sql语句大全之随机姓名生成方法
sql语句大全之SQL干货笔记
access数据库之随说秋色园从Access升迁到
access数据库之微信公众平台开发(26) ACCE
access数据库之ACCESS TOKEN
access数据库之当爬虫被拒绝时(Access Deni
access数据库之当爬虫被拒绝时(Access Deni
access数据库之使用PowerDesigner生成Access数据
access数据库之让ADO.NET Entity Framework 支持