asp.net数字报在防盗链方面的技术

广告:

程序代码

<%

’定义函数,用ADODB.Stream读取二进制数据

Function ReadBinaryFile(FileName)

Const adTypeBinary = 1

Dim BinaryStream

Set BinaryStream = CreateObject("ADODB.Stream")

BinaryStream.Type = adTypeBinary

BinaryStream.Open

BinaryStream.LoadFromFile FileName

ReadBinaryFile = BinaryStream.Read

End Function

Response.AddHeader "Content-Disposition", "attachment;filename=2.gif"’文件名

Response.ContentType = "image/GIF" ’设置(1)

response.Binarywrite ReadBinaryFile(server.mappath("2.gif"))’就是你读取存在本地的文件,防止被

别人知道真实路径盗连的。

%>

(1)下面的示例将 ContentType 属性设置为其他的常见值。

text/HTML 这个就不说了

image/GIF gif图片

image/JPEG jpg图片

application/x-cdf cdf文档

application/wma 就是西瓜哪个音乐类型了

具体可以参照 Web 浏览器文档或当前的 HTTP 规格说明

这样再利用asp的储存session,cookies,以及读取HTTP头等特殊功能就可以完全真正的实现防盗连,这里

没有设置缓存,如果访问量巨大,我想设置下就会更好吧。

下面是asp.net

7. 在Web.config中插入下面的配置

<appSettings>

<add key="uploadDirectory" value="uploads" />

</appSettings>

8. 写一个函数以下载文件(向客户端传递文件数据)

下载文件的代码

Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click

downloadfile("document.pdf") '

End Sub

'This is the function that you have to use

Private Function downloadfile(ByVal strFile As String)

Dim fs As FileStream

Dim strContentType As String

' This is the important part, because you going to use the local path

'to get the file

Dim strPath = Me.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings("uploadDirectory")) & "\"

Dim strFileName As String = strFile

fs = File.Open(strPath & strFileName, FileMode.Open)

Dim bytBytes(fs.Length) As Byte

fs.Read(bytBytes, 0, fs.Length)

fs.Close()

Response.AddHeader("Content-disposition","attachment; filename=" & strFileName)

Response.ContentType = "application/octet-stream"

Response.BinaryWrite(bytBytes)

Response.End()

Return True

End Function

广告:

相关内容

编辑:Admin 时间:2012/8/24 7:43:16 阅览:340   返回    
asp.net数字报
防盗链
扫描关注53BK报刊官网
扫描关注阅速公司微信