asp防盗链相关函数

  • 内容
  • 评论
  • 相关

如今网上关于asp的防盗链过程,想了很多方法,均不完美,
以下侧调用动网的防盗链,加上了一个关于图片的防盗链函数,根据文件类型合起来用,应该算是完美了!

防盗链下载函数 downloadFile 需要 服务器支持ADODB.Stream / fso

Sub downloadFile(strFile)
        On error resume next
        Server.ScriptTimeOut=999999
        Dim S,fso,f,intFilelength,strFilename,DownFileName

        strFilename = Server.MapPath(strFile)
        Response.Clear
        Set s = Server.CreateObject("ADODB.Stream")
        s.Open
        s.Type = 1
        Set fso = Server.CreateObject("Scripting.FileSystemObject")
        If Not fso.FileExists(strFilename) Then
            'Response.Write("<h1>错误: </h1><br>系统找不到指定文件")
            showimg "images/logos.gif"
            Exit Sub        
        End If
        Set f = fso.GetFile(strFilename)
            intFilelength = f.size
            s.LoadFromFile(strFilename)
            If err Then
                'Response.Write("<h1>错误: </h1>" & err.Description & "<p>")
                showimg "images/logos.gif"
                Response.End
            End If
            Set fso=Nothing
            Dim Data
            Data=s.Read
            s.Close
            Set s=Nothing
            If Response.IsClientConnected Then
                Response.AddHeader "Content-Disposition", "attachment; filename=" & strFile
                Response.AddHeader "Content-Length", intFilelength
                Response.CharSet = "UTF-8"
                Response.ContentType = "application/octet-stream"
                Response.BinaryWrite Data
                Response.Flush
            End If
    End Sub 

asp防盗链输出图片函数 showimg (需要Persits.Jpeg组件支持)

Sub showimg(FileName)
    Response.Clear
    Dim Jpeg,temp_pic
    On Error Resume Next
    Set Jpeg = Server.CreateObject("Persits.Jpeg")
    If -2147221005=Err then
        'Response.write "没有这个组件,请安装!" '检查是否安装aspJpeg组件
        downloadFile FileName
        Exit Sub
    End If
    Jpeg.Open (Server.MapPath(FileName)) '打开图片
    If err.number then
        'Response.write "打开图片失败,请检查路径!"
        Jpeg.Open (Server.MapPath("images/logos.gif"))
        response.end
    End if
    temp_pic=Jpeg.Binary
    Response.ContentType = "image/*"
    Response.AddHeader "Content-Disposition","filename=" & arr_FileName(FileName)
    Response.BinaryWrite temp_pic
    Response.End
End Sub

ps:来自己[烦恼's BLOG ]

评论

1条评论
  1. Gravatar 头像

    天缘博客

    博主对ASP研究的很深入,我不大懂ASP,用到时才会改一点,支持博主原创,常来学习

    [回复]

发表评论

电子邮件地址不会被公开。 必填项已用*标注

click to change 看不清?点击换一张!