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

看这个函数能帮到你吗

给你提供一点代码:

string fileURL = this.Server.MapPath("你要下载的文件路径")//文件路径,可用相对路径

FileInfo fileInfo = new FileInfo(fileURL)

Response.Clear()

Response.AddHeader("content-disposition", "attachmentfilename=" +

Server.UrlEncode(fileInfo.Name.ToString()))//文件名

Response.AddHeader("content-length", fileInfo.Length.ToString())//文件大小

Response.ContentType = "application/octet-stream"

Response.ContentEncoding = System.Text.Encoding.Default

Response.WriteFile(fileURL)

调用

response.Write("<a href=down.asp?filename="&UpLoadPath&ls_array(i+1)&">"&ls_array

(i)&"</td></tr>")

down.asp文件内容如下:

<%

Const FilePath = "UploadFile/" '文件存放路径

From_url = Cstr(Request.ServerVariables("HTTP_REFERER"))

Serv_url = Cstr(Request.ServerVariables("SERVER_NAME"))

Function GetFileName(longname)'/folder1/folder2/file.asp=>file.asp

while instr(longname,"/")

longname = right(longname,len(longname)-1)

wend

GetFileName = longname

End Function

Dim Stream

Dim Contents

Dim FileName

Dim TrueFileName

Dim FileExt

Const adTypeBinary = 1

FileName = Request.QueryString("FileName")

if FileName = "" Then

Response.Write "无效文件名!"

Response.End

End if

FileExt = Mid(FileName, InStrRev(FileName, ".") + 1)

Response.Clear

if lcase(right(FileName,3))="gif" or lcase(right(FileName,3))="jpg" or lcase(right(FileName,3))="png" then

Response.ContentType = "image/*" '对图像文件不出现下载对话框

else

Response.ContentType = "application/ms-download"

end if

Response.AddHeader "content-disposition", "attachmentfilename=" &GetFileName(Request.QueryString("FileName"))

Set Stream = server.CreateObject("ADODB.Stream")

Stream.Type = adTypeBinary

Stream.Open

TrueFileName= FilePath &FileName

Response.Write TrueFileName

Response.End

Stream.LoadFromFile Server.MapPath(TrueFileName)

While Not Stream.EOS

Response.BinaryWrite Stream.Read(1024 * 64)

Wend

Stream.Close

Set Stream = Nothing

Response.Flush

Response.End

%>


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存