个人博客,存放我个人的随笔,我常用的程序源码,分享软件等。
7 Jul
今天遇到了一个这样的问题:在下载页面也能够把图片下载下来。找了一个上午,终于找到了这样的例子,大家参考下吧!
程序说明:把下面的文件保存一个新页面,如download.asp,在需要下载的地方添加一个链接,这个链接地址就是要现在的文件的地址,如<a href="download.asp?downfilename=1.jpg">下载</a>。再具体的就自己看了!
<%@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")
If Not objFileSystem.FileExists(Path) Then
Msg "对不起,找不到您要下载的图片,请返回!","index.asp"
Exit Function
End If
Set objFile = objFileSystem.GetFile(Path)
Set objStream = objFile.OpenAsTextStream(ForReading, TristateTrue)
Response.AddHeader "content-type", mimeType
response.AddHeader "Content-Disposition","attachment;filename=" & 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
'信息输出函数
'Title 信息标题 Location 跳转地址(如果为空,则3秒钟跳转到上一页)
Function Msg(Title,Location)
If IsNull(Title) Or IsNull(Location) Then
Exit Function
End If
If Location="" Then
LocationType="javascript:history.go(-1)"
LocMsg="history.go(-1)"
Else
LocationType=Location
LocMsg="location.href='"&Location&"'"
End If
Msg="<script type=""text/javascript"">"&VBCRLF&"window.onload=function()"&VBCRLF&"{"&VBCRLF&"setTimeout("""&LocMsg&""",3000);"&VBCRLF&"}"&VBCRLF&"</script>"&VBCRLF
Msg=Msg&"<br /><br /><br /><br /><br />"&VBCRLF&"<table style=""font-size:12px; background:#CDE2F8; border:1px solid #8CBDEF;"" width=""500"" align=""center"" cellpadding=""3"" cellspacing=""1"">"&VBCRLF&"<tr style=""color: #FFFFFF; font-weight: bold; background-color: #8CBDEF;"">"&VBCRLF&"<td height=""25"">"&VBCRLF&"<div align=""center"">信息提示</div>"&VBCRLF&"</td>"&VBCRLF&"</tr>"&VBCRLF&"<tr bgcolor=""#FFFFFF"">"&VBCRLF&"<td height=""80"">"&VBCRLF&"<div align=""center""><br /><strong>"&Title&"</strong> <br /><br /><a href="""&LocationType&""" style=""color:#003366;"">如果您的浏览器没有自动跳转,请点击这里</a><br /><br /></div>"&VBCRLF&"</td>"&VBCRLF&"</tr>"&VBCRLF&"</table>"
Response.Write Msg
End Function
Dim path, mimeType, sucess,downfilename
downfilename=Request.QueryString("downfilename")'获取待下载的文件名
path = Server.MapPath(downfilename)
mimeType="text/plain"
sucess = TransferFile(path, mimeType,downfilename)
Response.End
%>
哇,找了好久,终于让我找到了,谢啦!
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。