'定义所有需要使用的变量
Dim strFilename,S,Fso,F,intFilelength
strFilename = Server.MapPath(Trim(Request("File")))
Response.Buffer = True
Response.Clear
Set S = Server.CreateObject("ADODB.Stream")
S.Open
S.Type = 1
On Error Resume Next
Set Fso = Server.CreateObject("Scripting.FileSystemObject")
If Not Fso.FileExists(strFilename) Then
Response.Write("<h1>Error:</h1>"&strFilename&"你要下载的文件不存在!<p>")
Response.End
End If
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
Response.AddHeader "Content-Disposition","attachment;filename="&F.name
Response.AddHeader "Content-Length",intFilelength
Response.CharSet = "GB2312"
Response.ContentType = "application/octet-stream"
Response.BinaryWrite S.Read
Response.Flush
这里添加一点说明,通过修改头部信息可以让浏览器知道是需要下载文件,另外上面的代码可以修改一下,
with response
end with就可以了
