在IE进行文档链接时如果遇到OLE支持的文档IE会自动调用相应程序打开它有时候这种功能并不是我们所需的虽然我们可以提醒用户用鼠标右键>目标另存为命令来下载文档但这样毕竟不太友好本文描述了利用FSO及Stream方法实现IE直接下载文档
<%@language=vbscriptcodepage=%>
<%
Filenamemustbeinput
ifRequest(Filename)=then
responsewrite<h>Error:</h>Filenameisempty!<p>
else
calldownloadFile(replace(replace(Request(Filename)\)/))
FunctiondownloadFile(strFile)
makesureyouareonthelatestMDACversionforthistowork
getfullpathofspecifiedfile
strFilename=serverMapPath(strFile)
clearthebuffer
ResponseBuffer=True
ResponseClear
createstream
Sets=ServerCreateObject(ADODBStream)
sOpen
Setasbinary
sType=
loadinthefile
onerrorresumenext
checkthefileexists
Setfso=ServerCreateObject(ScriptingFileSystemObject)
ifnotfsoFileExists(strFilename)then
ResponseWrite(<h>Error:</h>&strFilename&doesnotexists!<p>)
ResponseEnd
endif
getlengthoffile
Setf=fsoGetFile(strFilename)
intFilelength=fsize
sLoadFromFile(strFilename)
iferrthen
ResponseWrite(<h>Error:</h>UnknownError!<p>)
ResponseEnd
endif
sendtheheaderstotheusersBrowse
ResponseAddHeaderContentDispositionattachment;filename=&fname
ResponseAddHeaderContentLengthintFilelength
ResponseCharSet=UTF
ResponseContentType=application/octetstream
outputthefiletothebrowser
ResponseBinaryWritesRead
ResponseFlush
tidyup
sClose
Sets=Nothing
EndFunction
endif
%>