最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > asp教程

asp中自定义文件下载技巧

日期:11-24    来源:中国设计秀    作者:cnwebshow.com

可以用流下载(耗内存,少用)或直接转到该文件.AzE中国设计秀

AzE中国设计秀
<%AzE中国设计秀

Const USE_STREAM = 0 '0.不用流(Adodb.Stream)下载 1.用流下载AzE中国设计秀
Const ALLOW_FILE_EXT = "rar,zip,chm,doc,xls,swf,mp3,gif,jpg,jpeg,png,bmp" '允许下载的文件的扩展名,防止源代码被下载AzE中国设计秀

Dim sDownFilePath '下载文件路径AzE中国设计秀
sDownFilePath = Trim(Request("FilePath"))AzE中国设计秀
'或者根据传过来的文件ID从数据库中获取文件路径AzE中国设计秀

'如果 sDownFilePath 为绝对路径,一定要将 sDownFilePath 转换为相对 本文件的相对路径AzE中国设计秀

'sDownFilePath = "focus.swf"AzE中国设计秀

Call DownloadFile(sDownFilePath)AzE中国设计秀

Function DownloadFile(s_DownFilePath)AzE中国设计秀
    '判断有没传递文件名AzE中国设计秀
    If IsNull(s_DownFilePath) = True Or Trim(s_DownFilePath) = "" ThenAzE中国设计秀
        OutputErr "错误:先确定要下载的文件,下载失败"AzE中国设计秀
    End IfAzE中国设计秀

    '判断扩展名是否合法AzE中国设计秀
    Dim s_FileExtAzE中国设计秀
    s_FileExt = Mid(s_DownFilePath, InstrRev(s_DownFilePath, ".")+1)AzE中国设计秀
    If InStr("," & ALLOW_FILE_EXT & ",", "," & s_FileExt & ",") <= 0 ThenAzE中国设计秀
        OutputErr "错误:文件类型(" & s_FileExt & ")不允许被下载,下载失败"AzE中国设计秀
    End IfAzE中国设计秀
    AzE中国设计秀
    s_DownFilePath = Replace(s_DownFilePath, "", "/")AzE中国设计秀

    '为了安全,某些目录禁止下载文件,在这里处理AzE中国设计秀
    'AzE中国设计秀
    AzE中国设计秀
    '检测服务器是否支持fsoAzE中国设计秀
    Dim o_FsoAzE中国设计秀
    On Error Resume NextAzE中国设计秀
    Set o_Fso = Server.CreateObject("Scripting.FileSystemObject")AzE中国设计秀
    If Err.Number <> 0 ThenAzE中国设计秀
        Err.ClearAzE中国设计秀
        OutputErr "错误:服务器不支持fso组件,下载失败"AzE中国设计秀
    End IfAzE中国设计秀

    '取得文件名,文件大小AzE中国设计秀
    Dim s_FileMapPathAzE中国设计秀
    Dim o_File, s_FileName, n_FileLengthAzE中国设计秀
    s_FileMapPath = Server.MapPath(s_DownFilePath)AzE中国设计秀
    If (o_Fso.FileExists(s_FileMapPath)) = True ThenAzE中国设计秀
        Set o_File = o_Fso.GetFile(s_FileMapPath)AzE中国设计秀
        s_FileName = o_File.NameAzE中国设计秀
        n_FileLength = o_File.SizeAzE中国设计秀
        o_File.CloseAzE中国设计秀
    ElseAzE中国设计秀
        OutputErr "错误:文件不存在,下载失败"AzE中国设计秀
    End IfAzE中国设计秀
    Set o_Fso = NothingAzE中国设计秀

    '判断是否下载的文件大小超过限制AzE中国设计秀
    '    AzE中国设计秀
    AzE中国设计秀
    '如果不是用流下载,直接转到该文件AzE中国设计秀
    If USE_STREAM = 0 ThenAzE中国设计秀
        Response.Redirect sDownFilePathAzE中国设计秀
        Response.endAzE中国设计秀
    End IfAzE中国设计秀

    '检测服务器是否支持Adodb.StreamAzE中国设计秀
    On Error Resume NextAzE中国设计秀
    Set o_Stream = Server.CreateObject("Adodb.Stream")AzE中国设计秀
    If Err.Number <> 0 ThenAzE中国设计秀
        Err.ClearAzE中国设计秀
        OutputErr "错误:服务器不支持Adodb.Stream组件,下载失败"AzE中国设计秀
    End IfAzE中国设计秀

    o_Stream.Tyep = 1AzE中国设计秀
    o_Stream.OpenAzE中国设计秀
    o_Stream.LoadFromFile s_FileMapPathAzE中国设计秀

    Response.Buffer = TrueAzE中国设计秀
    Response.ClearAzE中国设计秀
    Response.AddHeader "Content-Disposition", "attachment; filename=" & s_FileNameAzE中国设计秀
    Response.AddHeader "Content-Length", n_FileLength AzE中国设计秀
    Response.CharSet = "UTF-8" AzE中国设计秀
    Response.ContentType = "application/octet-stream" AzE中国设计秀
    Response.BinaryWrite o_Stream.ReadAzE中国设计秀
    Response.FlushAzE中国设计秀

    o_Stream.CloseAzE中国设计秀
    Set o_Stream = NothingAzE中国设计秀

End FunctionAzE中国设计秀

Sub OutputErr(s_ErrMsg)AzE中国设计秀
    Response.Write "<font color=red>" & s_ErrMsg & "</font>" AzE中国设计秀
    Response.EndAzE中国设计秀
End SubAzE中国设计秀

%>AzE中国设计秀

http://www.cnblogs.com/jiny-z/archive/2006/08/29/489102.htmlAzE中国设计秀

本文引用地址:/bc/article_46473.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明