赶知识网

ASP实现文件下载(弹出保存对话框)

asp/access/IIS/mssql 2013-06-18发布 3300次点击
第一个:


      '    描述: 弹出保存对话框, 而不是直接查看, 防止暴露文件路径
      '    参数: fullpath : 文件在服务器上的完整物理路径
      '    返回: true/false : 下载是否成功
      Function downloadfile(fullpath)
          downloadfile = False

          Dim strfilename, s, fso, f, intfilelength

          Set fso = server.createobject("scripting.filesystemobject")
          If not fso.fileexists(fullpath) Then
              Exit Function
          End If

          Set f = fso.getfile(fullpath)
          '获取文件大小
          intfilelength = f.size

          Set s = server.createobject("adodb.stream")
          s.open
          s.type = 1
          s.loadfromfile(fullpath)

          response.buffer = True
          response.clear
          'response.addheader "content-type","application/x-msdownload"
          'response.addheader "Content-Encoding","GB2312"
          response.addheader "content-disposition","attachment;filename=" & f.name
          response.addheader "content-length" ,intfilelength
          response.contenttype = "application/octet-stream"
          While not s.eos
              response.binarywrite s.read(1024 * 64)
            '    关键的一句
              response.flush
          wend
          s.close
          Set s = Nothing

          downloadfile = True
      End Function

第二个:

Dim Stream
Dim Contents
Dim FileName
Dim FileExt
Const adTypeBinary = 1
FileName=Request.QueryString("FileName")
if FileName = "" Then
Response.Write "无效文件名."
Response.End
End if
' 下面是不希望下载的文件
FileExt = Mid(FileName, InStrRev(FileName, ".") + 1)
Select Case UCase(FileExt)
Case "ASP", "ASA", "ASPX", "ASAX", "MDB"
Response.Write "受保护文件,不能下载."
Response.End
End Select
' 下载这个文件

name=Mid(FileName, InStrRev(FileName, "/")+1)

Response.Clear
Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition", "attachment; filename="&name
Set Stream = server.CreateObject("ADODB.Stream")
Stream.Type = adTypeBinary
Stream.Open
Stream.LoadFromFile Server.MapPath(FileName)
While Not Stream.EOS
Response.BinaryWrite Stream.Read(1024 * 64)
Wend
Stream.Close
Set Stream = Nothing
Response.Flush
Response.End

Top10

沪ICP备09053415号 © 赶知识网