ASP如何实现点击链接弹出图片另存为功能
请问用ASP如何实现点击链接弹出“图片另存为对话框”的功能?我有个图片网站,现在网友只能鼠标右键点图片另存为保存图片。我想提升下用户体验,直接让网友点击保存链接这几个字就...
请问用ASP 如何实现 点击链接弹出“图片另存为对话框”的功能?
我有个图片网站,现在网友只能鼠标右键点图片另存为保存图片。我想提升下用户体验,直接让网友点击保存链接 这几个字就能弹出保存图片的对话框,然后可以保存到本地电脑上。
我在网上找了一段代码,但是不知道如何修改。麻烦大家帮忙解决下,告诉我该如何修改。
请说明如何实现
call download(server.mappath("./test.jpg"))
Function download(File)
temp = Split(File, "\")
filename = temp(UBound(temp))
Set s = CreateObject("adodb.stream")
s.mode = 3
s.Type = 1
s.Open
s.loadfromfile(File)
data = s.Read
If IsNull(data) Then
response.Write "空"
Else
response.Clear
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", "attachment; filename=" & filename
response.binarywrite(data)
End If
Set s = Nothing
End Function 展开
我有个图片网站,现在网友只能鼠标右键点图片另存为保存图片。我想提升下用户体验,直接让网友点击保存链接 这几个字就能弹出保存图片的对话框,然后可以保存到本地电脑上。
我在网上找了一段代码,但是不知道如何修改。麻烦大家帮忙解决下,告诉我该如何修改。
请说明如何实现
call download(server.mappath("./test.jpg"))
Function download(File)
temp = Split(File, "\")
filename = temp(UBound(temp))
Set s = CreateObject("adodb.stream")
s.mode = 3
s.Type = 1
s.Open
s.loadfromfile(File)
data = s.Read
If IsNull(data) Then
response.Write "空"
Else
response.Clear
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", "attachment; filename=" & filename
response.binarywrite(data)
End If
Set s = Nothing
End Function 展开
2个回答
展开全部
DOWNLOAD.HTM
<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","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
Dim path, mimeType, sucess,downfilename
downfilename=request("filename")
path = Server.MapPath(downfilename)
mimeType="text/plain"
sucess = TransferFile(path, mimeType,downfilename)
Response.End
%>
以上是下载个ASP文件,图片是一样。
下面是一个ASP解决方案:
文件名:Download.asp
<%
Dim Stream
Dim Contents
Dim FileName
Dim FileExt
Const adTypeBinary = 1
FileName = Request.QueryString("FileName")
if FileName = "" Then
Response.Write "无效文件名."
Response.End
End if
' 下面是不希望下载的文件
FileExt = Mid(FileName, InStrRev(FileName, ".") + 1)
Select Case UCase(FileExt)
Case "ASP", "ASA", "ASPX", "ASAX", "MDB"
Response.Write "受保护文件,不能下载."
Response.End
End Select
' 下载这个文件
Response.Clear
Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition", "attachment; filename=" & FileName
Set Stream = server.CreateObject("ADODB.Stream")
Stream.Type = adTypeBinary
Stream.Open
Stream.LoadFromFile Server.MapPath(FileName)
While Not Stream.EOS
Response.BinaryWrite Stream.Read(1024 * 64)
Wend
Stream.Close
Set Stream = Nothing
Response.Flush
Response.End
%>
使用:Download.asp?FileName=/Files/MY.gif
把你的文件放到根目录Files下,你也可以放到其它地方了。
如:
<A HREF="Download.asp?FileName=/Files/MY.gif">点击下载WORD文档</A>
<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","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
Dim path, mimeType, sucess,downfilename
downfilename=request("filename")
path = Server.MapPath(downfilename)
mimeType="text/plain"
sucess = TransferFile(path, mimeType,downfilename)
Response.End
%>
以上是下载个ASP文件,图片是一样。
下面是一个ASP解决方案:
文件名:Download.asp
<%
Dim Stream
Dim Contents
Dim FileName
Dim FileExt
Const adTypeBinary = 1
FileName = Request.QueryString("FileName")
if FileName = "" Then
Response.Write "无效文件名."
Response.End
End if
' 下面是不希望下载的文件
FileExt = Mid(FileName, InStrRev(FileName, ".") + 1)
Select Case UCase(FileExt)
Case "ASP", "ASA", "ASPX", "ASAX", "MDB"
Response.Write "受保护文件,不能下载."
Response.End
End Select
' 下载这个文件
Response.Clear
Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition", "attachment; filename=" & FileName
Set Stream = server.CreateObject("ADODB.Stream")
Stream.Type = adTypeBinary
Stream.Open
Stream.LoadFromFile Server.MapPath(FileName)
While Not Stream.EOS
Response.BinaryWrite Stream.Read(1024 * 64)
Wend
Stream.Close
Set Stream = Nothing
Response.Flush
Response.End
%>
使用:Download.asp?FileName=/Files/MY.gif
把你的文件放到根目录Files下,你也可以放到其它地方了。
如:
<A HREF="Download.asp?FileName=/Files/MY.gif">点击下载WORD文档</A>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |