« 银联电子支付站点配置Windows2003序列号 »

让TXT文本等其他任意格式的文件,不在IE中打开,而直接下载的方法

分类: 网页设计代码 发布: roger 浏览: 日期: 2012年4月20日

ASP实现不打开直接下载txt,jpg...类文件

代码:

 


<%
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
%>

把此代码保存为down.asp

使用方法

<a href=down.asp?filename=test.jpg>下载图片</a>
<a href=down.asp?filename=test.txt>下载文本</a>

注意:test.jpg是放在与down.asp同一目录下

相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-Blog 1.8 Walle Build 91204

Copyright 2008-2022 WWW.XMHJFB.COM Rights Reserved 闽ICP备16020319号