ASP写的文件下载代码

ASP写的文件下载代码,第1张

分类: 电脑/网络 >>程序设计 >>其他编程语言

问题描述:

在软件信息上有一个下载按钮:

<a href="down?filename=<%=rs("filename")%>"><img src=images/download.gif border=0></a>

在down上计算下载次数,并下载文件

<!--#include file="conn"-->

<%

set rs2 = server.CreateObject ("Adodb.recordset")

sql2="update downtimes set downtimes=(downtimes+1) where filename="&request("filename")

rs2.open sql2,conn,1,1

set rs2=nothing

set rs = server.CreateObject ("Adodb.recordset")

sql="select * from upfile where filename="&request("filename")

rs.open sql,conn,1,1

Response.Redirect rs("filepath")

%>

filepath一般是“XXX.mp3”或“XXX.wma”、“XXX.doc”、“XXX”这样的名称,当点击下载按钮后,浏览器不显示下载对话框,而是直接在浏览器中播放或是显示这些文档或图片。请问有没有什么办法,不让浏览器调用本机播放器播放这些影音文件。当然,右键“目标另存为”可以下载,但是这样就不能计算文件的下载次数了,有两全其美的办法么?

解析:

Function downloadFile(strFile)

strFilename = server.MapPath(strFile)

Response.Buffer = True

Response.Clear

Set s = Server.CreateObject("ADODB.Stream")

s.Open

s.Type = 1

on error resume next

Set fso = Server.CreateObject("Scripting.FileSystemObject")

if not fso.FileExists(strFilename) then

Response.Write("<h1>Error:</h1>文件不存在<p>")

Response.End

end if

Set f = fso.GetFile(strFilename)

intFilelength = f.size

s.LoadFromFile(strFilename)

if err then

Response.Write("<h1>Error: </h1>" &err.Description &"<p>")

Response.End

end if

Response.AddHeader "Content-Disposition", "attachmentfilename=" &f.name

Response.AddHeader "Content-Length", intFilelength

Response.CharSet = "UTF-8"

Response.ContentType = "application/octet-stream"

Response.BinaryWrite s.Read

Response.Flush

s.Close

Set s = Nothing

End Function

看这个函数能帮到你吗

<a href=download.asp?filename=download.asp>guid.asp</a>

以下另存为:DOWNLOAD.ASP

<%@language=VBscript%>

<%

Const ForReading=1

Const TristateTrue=-1

Const FILE_TRANSFER_SIZE=16384

Response.Buffer = True

Function TransferFile(path, mimeType, filename)

Dim objFileSystem, objFile, objStream

Dim char

Dim sent

send=0

TransferFile = True

Set objFileSystem = Server.CreateObject("Scripting.FileSystemObject")

Set objFile = objFileSystem.GetFile(Path)

Set objStream = objFile.OpenAsTextStream(ForReading, TristateTrue)

Response.AddHeader "content-type", mimeType

response.AddHeader "Content-Disposition","attachmentfilename=" &filename

Response.AddHeader "content-length", objFile.Size

Do While Not objStream.AtEndOfStream

char = objStream.Read(1)

Response.BinaryWrite(char)

sent = sent + 1

If (sent MOD FILE_TRANSFER_SIZE) = 0 Then

Response.Flush

If Not Response.IsClientConnected Then

TransferFile = False

Exit Do

End If

End If

Loop

Response.Flush

If Not Response.IsClientConnected Then TransferFile = False

objStream.Close

Set objStream = Nothing

Set objFileSystem = Nothing

End Function

Dim path, mimeType, sucess,downfilename

downfilename=request("filename")

path = Server.MapPath(downfilename)

mimeType="text/plain"

sucess = TransferFile(path, mimeType,downfilename)

Response.End

%>

ASP网站放在网络服务器上的,我们打开网站看到的网页是结果编译的HTML文件。如何对他进行整站下载就比如说那个“查看”-》“源文件”,或是用某些软件进行整站下载的,这些载下来的文件都不是真真的源文件代码而是静态HTML网页代码。如果真的谁都可以随便就把ASP整站载下来那服务器漏洞安全性也太差了吧。所以正常手段想ASP整站载下来的是不可能的。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/7985066.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存