如何利用ASP来防止网站文件被盗链

2025-05-14 20:43:18
推荐回答(2个)
回答1:

方法--郑州百林鸟 网站

1. 创建你的网站
2. 创建一个上传文件的目录(在网站中),这里我们用UPLOADS作为例子
3. 在IIS中,选择你的UPLOADS目录
4. 右键->属性
5. 选择directory标签,确保只允许写的访问权限。
于是,当如果有非法用户想直接键入文件的URL,来下载文件,就会得到错误了。

6. 插入一个Link Button或者Button,用以下载文件

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




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

这个函数我测试了300MB的,不过好像650MB的文件会有问题。

回答2:

安装上360就带有ASP