可以用流下载(耗内存,少用)或直接转到该文件.
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
<%
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Const USE_STREAM = 0 '0.不用流(Adodb.Stream)下载 1.用流下载
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Const ALLOW_FILE_EXT = "rar,zip,chm,doc,xls,swf,mp3,gif,jpg,jpeg,png,bmp" '允许下载的文件的扩展名,防止源代码被下载
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Dim sDownFilePath '下载文件路径
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
sDownFilePath = Trim(Request("FilePath"))
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
'或者根据传过来的文件ID从数据库中获取文件路径
'如果 sDownFilePath 为绝对路径,一定要将 sDownFilePath 转换为相对 本文件的相对路径
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
'sDownFilePath = "focus.swf"
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Call DownloadFile(sDownFilePath)
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Function DownloadFile(s_DownFilePath)
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
'判断有没传递文件名
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
If IsNull(s_DownFilePath) = True Or Trim(s_DownFilePath) = "" Then
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
OutputErr "错误:先确定要下载的文件,下载失败"
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
End If
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
'判断扩展名是否合法
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Dim s_FileExt
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
s_FileExt = Mid(s_DownFilePath, InstrRev(s_DownFilePath, ".")+1)
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
If InStr("," & ALLOW_FILE_EXT & ",", "," & s_FileExt & ",") <= 0 Then
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
OutputErr "错误:文件类型(" & s_FileExt & ")不允许被下载,下载失败"
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
End If
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
s_DownFilePath = Replace(s_DownFilePath, "\", "/")
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
'为了安全,某些目录禁止下载文件,在这里处理
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
'
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
'检测服务器是否支持fso
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Dim o_Fso
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
On Error Resume Next
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Set o_Fso = Server.CreateObject("Scripting.FileSystemObject")
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
If Err.Number <> 0 Then
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Err.Clear
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
OutputErr "错误:服务器不支持fso组件,下载失败"
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
End If
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
'取得文件名,文件大小
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Dim s_FileMapPath
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Dim o_File, s_FileName, n_FileLength
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
s_FileMapPath = Server.MapPath(s_DownFilePath)
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
If (o_Fso.FileExists(s_FileMapPath)) = True Then
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Set o_File = o_Fso.GetFile(s_FileMapPath)
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
s_FileName = o_File.Name
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
n_FileLength = o_File.Size
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
o_File.Close
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Else
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
OutputErr "错误:文件不存在,下载失败"
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
End If
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Set o_Fso = Nothing
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
'判断是否下载的文件大小超过限制
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
'
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
'如果不是用流下载,直接转到该文件
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
If USE_STREAM = 0 Then
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Response.Redirect sDownFilePath
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Response.end
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
End If
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
'检测服务器是否支持Adodb.Stream
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
On Error Resume Next
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Set o_Stream = Server.CreateObject("Adodb.Stream")
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
If Err.Number <> 0 Then
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Err.Clear
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
OutputErr "错误:服务器不支持Adodb.Stream组件,下载失败"
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
End If
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
o_Stream.Type = 1
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
o_Stream.Open
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
o_Stream.LoadFromFile s_FileMapPath
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Response.Buffer = True
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Response.Clear
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Response.AddHeader "Content-Disposition", "attachment; filename=" & s_FileName
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Response.AddHeader "Content-Length", n_FileLength
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Response.CharSet = "UTF-8"
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Response.ContentType = "application/octet-stream"
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Response.BinaryWrite o_Stream.Read
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Response.Flush
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
o_Stream.Close
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Set o_Stream = Nothing
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
End Function
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Sub OutputErr(s_ErrMsg)
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Response.Write "<font color=red>" & s_ErrMsg & "</font>"
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
Response.End
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
End Sub
![](http://img.e-com-net.com/image/product/7d98e82dc0f34e1783f28359905e0d7e.gif)
%>