asp在IE浏览器中下载服务端上的各类文件的实现方法
程序员文章站
2022-07-01 23:47:07
即直接提示用户下载而不是由浏览器打开某些文件。注意,下面的代码拷贝到asp文件中后,不要再添加一些非asp代码在页面中:如html和javascript客户端的代码。&nb...
即直接提示用户下载而不是由浏览器打开某些文件。注意,下面的代码拷贝到asp文件中后,不要再添加一些非asp代码在页面中:如html和javascript客户端的代码。
<%
'--------------------------------------------
response.buffer = true
dim strfilepath, strfilesize, strfilename
const adtypebinary = 1
strfilepath = "文件路径 "
strfilesize = ... 文件大小,可选
strfilename = "文件名"
response.clear
'8*******************************************8
' 需要在你的服务器上安装 mdac 2.6 或mdac2.7
'8*******************************************8
set objstream = server.createobject("adodb.stream")
objstream.open
objstream.type = adtypebinary
objstream.loadfromfile strfilepath
strfiletype = lcase(right(strfilename, 4)) '文件扩展名 站.长.站
' 通过文件扩展名判断 content-types
select case strfiletype
case ".asf"
contenttype = "video/x-ms-asf"
case ".avi"
contenttype = "video/avi"
case ".doc"
contenttype = "application/msword"
case ".zip"
contenttype = "application/zip"
case ".xls"
contenttype = "application/vnd.ms-excel"
case ".gif"
contenttype = "image/gif"
case ".jpg", "jpeg"
contenttype = "image/jpeg"
case ".wav"
contenttype = "audio/wav"
case ".mp3"
contenttype = "audio/mpeg3"
case ".mpg", "mpeg"
contenttype = "video/mpeg"
case ".rtf"
contenttype = "application/rtf"
case ".htm", "html"
contenttype = "text/html"
case ".asp"
contenttype = "text/asp"
case else
'handle all other files
contenttype = "application/octet-stream"
end select
response.addheader "content-disposition", "attachment; filename= strfilename
response.addheader "content-length", strfilesize
response.charset = "utf-8" ' 客户端浏览器的字符集utf-8
response.contenttype = contenttype
response.binarywrite objstream.read
response.flush
objstream.close
set objstream = nothing
%>
复制代码 代码如下:
<%
'--------------------------------------------
response.buffer = true
dim strfilepath, strfilesize, strfilename
const adtypebinary = 1
strfilepath = "文件路径 "
strfilesize = ... 文件大小,可选
strfilename = "文件名"
response.clear
'8*******************************************8
' 需要在你的服务器上安装 mdac 2.6 或mdac2.7
'8*******************************************8
set objstream = server.createobject("adodb.stream")
objstream.open
objstream.type = adtypebinary
objstream.loadfromfile strfilepath
strfiletype = lcase(right(strfilename, 4)) '文件扩展名 站.长.站
' 通过文件扩展名判断 content-types
select case strfiletype
case ".asf"
contenttype = "video/x-ms-asf"
case ".avi"
contenttype = "video/avi"
case ".doc"
contenttype = "application/msword"
case ".zip"
contenttype = "application/zip"
case ".xls"
contenttype = "application/vnd.ms-excel"
case ".gif"
contenttype = "image/gif"
case ".jpg", "jpeg"
contenttype = "image/jpeg"
case ".wav"
contenttype = "audio/wav"
case ".mp3"
contenttype = "audio/mpeg3"
case ".mpg", "mpeg"
contenttype = "video/mpeg"
case ".rtf"
contenttype = "application/rtf"
case ".htm", "html"
contenttype = "text/html"
case ".asp"
contenttype = "text/asp"
case else
'handle all other files
contenttype = "application/octet-stream"
end select
response.addheader "content-disposition", "attachment; filename= strfilename
response.addheader "content-length", strfilesize
response.charset = "utf-8" ' 客户端浏览器的字符集utf-8
response.contenttype = contenttype
response.binarywrite objstream.read
response.flush
objstream.close
set objstream = nothing
%>