下载 ConstUSE_STREAM=0'0.不用流(Adodb.Stream)下载1.用流下载 DimsDownFilePath'下载文件路径 '如果sDownFilePath为绝对路径,一定要将sDownFilePath转换为相对本文件的相对路径 'sDownFilePath="focus.swf" CallDownloadFile(sDownFilePath) FunctionDownloadFile(s_DownFilePath) '判断扩展名是否合法 '为了安全,某些目录禁止下载文件,在这里处理 '取得文件名,文件大小 '判断是否下载的文件大小超过限制 '检测服务器是否支持Adodb.Stream o_Stream.Tyep=1 Response.Buffer=True o_Stream.Close EndFunction SubOutputErr(s_ErrMsg) %> http://www.cnblogs.com/jiny-z/archive/2006/08/29/489102.html
<%
ConstALLOW_FILE_EXT="rar,zip,chm,doc,xls,swf,mp3,gif,jpg,jpeg,png,bmp"'允许下载的文件的扩展名,防止源代码被下载
sDownFilePath=Trim(Request("FilePath"))
'或者根据传过来的文件ID从数据库中获取文件路径
'判断有没传递文件名
IfIsNull(s_DownFilePath)=TrueOrTrim(s_DownFilePath)=""Then
OutputErr"错误:先确定要下载的文件,下载失败"
EndIf
Dims_FileExt
s_FileExt=Mid(s_DownFilePath,InstrRev(s_DownFilePath,".")+1)
IfInStr(","&ALLOW_FILE_EXT&",",","&s_FileExt&",")<=0Then
OutputErr"错误:文件类型("&s_FileExt&")不允许被下载,下载失败"
EndIf
s_DownFilePath=Replace(s_DownFilePath,"","/")
'
'检测服务器是否支持fso
Dimo_Fso
OnErrorResumeNext
Seto_Fso=Server.CreateObject("Scripting.FileSystemObject")
IfErr.Number<>0Then
Err.Clear
OutputErr"错误:服务器不支持fso组件,下载失败"
EndIf
Dims_FileMapPath
Dimo_File,s_FileName,n_FileLength
s_FileMapPath=Server.MapPath(s_DownFilePath)
If(o_Fso.FileExists(s_FileMapPath))=TrueThen
Seto_File=o_Fso.GetFile(s_FileMapPath)
s_FileName=o_File.Name
n_FileLength=o_File.Size
o_File.Close
Else
OutputErr"错误:文件不存在,下载失败"
EndIf
Seto_Fso=Nothing
'
'如果不是用流下载,直接转到该文件
IfUSE_STREAM=0Then
Response.RedirectsDownFilePath
Response.end
EndIf
OnErrorResumeNext
Seto_Stream=Server.CreateObject("Adodb.Stream")
IfErr.Number<>0Then
Err.Clear
OutputErr"错误:服务器不支持Adodb.Stream组件,下载失败"
EndIf
o_Stream.Open
o_Stream.LoadFromFiles_FileMapPath
Response.Clear
Response.AddHeader"Content-Disposition","attachment;filename="&s_FileName
Response.AddHeader"Content-Length",n_FileLength
Response.CharSet="UTF-8"
Response.ContentType="application/octet-stream"
Response.BinaryWriteo_Stream.Read
Response.Flush
Seto_Stream=Nothing
Response.Write"<fontcolor=red>"&s_ErrMsg&"</font>"
Response.End
EndSub
asp中自定义文件下载
80酷酷网 80kuku.com