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>

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

阅读更多
文章标签: asp path function file
个人分类: ASP/ASP.NET开发
想对作者说点什么? 我来说一句

asp.net文件下载源代码

2009年10月19日 180KB 下载

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭