asp中自訂檔案下載方式.

來源:互聯網
上載者:User
可以用流下載(耗記憶體,少用)或直接轉到該檔案.


<%

Const USE_STREAM = 0 '0.不用流(Adodb.Stream)下載 1.用流下載
Const ALLOW_FILE_EXT = "rar,zip,chm,doc,xls,swf,mp3,gif,jpg,jpeg,png,bmp" '允許下載的檔案的副檔名,防止原始碼被下載

Dim sDownFilePath '下載檔案路徑
sDownFilePath = Trim(Request("FilePath"))
'或者根據傳過來的檔案ID從資料庫中擷取檔案路徑

'如果 sDownFilePath 為絕對路徑,一定要將 sDownFilePath 轉換為相對 本檔案的相對路徑

'sDownFilePath = "focus.swf"

Call DownloadFile(sDownFilePath)

Function DownloadFile(s_DownFilePath)
    '判斷有沒傳遞檔案名稱
    If IsNull(s_DownFilePath) = True Or Trim(s_DownFilePath) = "" Then
        OutputErr "錯誤:先確定要下載的檔案,下載失敗"
    End If

    '判斷副檔名是否合法
    Dim s_FileExt
    s_FileExt = Mid(s_DownFilePath, InstrRev(s_DownFilePath, ".")+1)
    If InStr("," & ALLOW_FILE_EXT & ",", "," & s_FileExt & ",") <= 0 Then
        OutputErr "錯誤:檔案類型(" & s_FileExt & ")不允許被下載,下載失敗"
    End If
    
    s_DownFilePath = Replace(s_DownFilePath, "\", "/")

    '為了安全,某些目錄禁止下載檔案,在這裡處理
    '
    
    '檢測伺服器是否支援fso
    Dim o_Fso
    On Error Resume Next
    Set o_Fso = Server.CreateObject("Scripting.FileSystemObject")
    If Err.Number <> 0 Then
        Err.Clear
        OutputErr "錯誤:伺服器不支援fso組件,下載失敗"
    End If

    '取得檔案名稱,檔案大小
    Dim s_FileMapPath
    Dim o_File, s_FileName, n_FileLength
    s_FileMapPath = Server.MapPath(s_DownFilePath)
    If (o_Fso.FileExists(s_FileMapPath)) = True Then
        Set o_File = o_Fso.GetFile(s_FileMapPath)
        s_FileName = o_File.Name
        n_FileLength = o_File.Size
        o_File.Close
    Else
        OutputErr "錯誤:檔案不存在,下載失敗"
    End If
    Set o_Fso = Nothing

    '判斷是否下載的檔案大小超過限制
    '    
    
    '如果不是用流下載,直接轉到該檔案
    If USE_STREAM = 0 Then
        Response.Redirect sDownFilePath
        Response.end
    End If

    '檢測伺服器是否支援Adodb.Stream
    On Error Resume Next
    Set o_Stream = Server.CreateObject("Adodb.Stream")
    If Err.Number <> 0 Then
        Err.Clear
        OutputErr "錯誤:伺服器不支援Adodb.Stream組件,下載失敗"
    End If

    o_Stream.Type = 1
    o_Stream.Open
    o_Stream.LoadFromFile s_FileMapPath 

    Response.Buffer = True
    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.BinaryWrite o_Stream.Read
    Response.Flush

    o_Stream.Close
    Set o_Stream = Nothing

End Function

Sub OutputErr(s_ErrMsg)
    Response.Write "<font color=red>" & s_ErrMsg & "</font>" 
    Response.End
End Sub

%>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.