返回列表 发帖

用asp直接下载文件的办法,不用再在IE中打开了

代码:
<%
strFilename = Trim(Request("n"))
Function DownloadFile(strFile,filename)
strFilename = server.MapPath(strFile)
'清空Buffer
Response.Buffer = True
Response.Clear
'创建Stream对象
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
downloadFile="NoFile"
Exit Function
End If
'计算文件长度
Set f = fso.GetFile(strFilename)
intFilelength = f.size
If filename="" Then
filename=f.name
End If
s.LoadFromFile(strFilename)
if err then
Response.Write("<h1>Error: </h1>" & err.Description & "<p>")
Response.End
end if
'向用户浏览器发送Header
Response.AddHeader "Content-Disposition", "attachment; filename=" & filename
Response.AddHeader "Content-Length", intFilelength
Response.CharSet = "UTF-8"
Response.ContentType = "application/octet-stream"
'输出文件
'对于小于4096KB的文件可以用语句
'Response.BinaryWrite s.Read
'Response.Flush
'完成,但对于大于4096KB的文件要分段输出,如下循环操作。
Do While Not s.EOS
  Contents = s.Read (4096) '每次读取4096KB
Response.BinaryWrite Contents
Response.Flush
Loop

'清理
s.Close
Set s = Nothing
End Function
call downloadFile(strFilename,strFilename)
%>

返回列表