asp实现jpg、txt、html文件直接下载

今天有网友向我求助,希望解决在asp实现jpg、txt、html文件直接下载的难题,同时希望有详细的asp代码。我们都清楚,如果在网页中下载jpg格式文件会通过IE自动打开的,无法实现点击下载jpg文件,txt、html、asp等文件也是一样IE会自动打开,如何在asp实现jpg、txt、html文件直接下载呢,这个代码又怎么写呢,下面是从学无忧转载过来的几种程序代码及使用方法:

一、程序代码

asp文件直接下载代码一:

如果你只需要实现下载jpg、txt、html文件,可以采用这种简单的代码,代码如下:

 <%

url=request("filename")

Response.AddHeader "content-type","application/x-msdownload"

Response.AddHeader "Content-Disposition","attachment;filename=" & url

Response.End()

%>

asp文件直接下载代码二:

如果你要实现不但可以下载jpg、txt、html格式文件,同时还希望能够直接下载asp、php等格式文件下载,那么可以用下面的代码来实现,代码如下:

 <%

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文件直接下载代码三:

以下程序代码同样可以下载任何文件格式,包含jpg、html、asp、php等,代码如下:

 <%   
function download(f,n)  
on error resume next  
Set S=CreateObject("Adodb.Stream")  
S.Mode=3  
S.Type=1  
S.Open  
S.LoadFromFile(f)  
if Err.Number>0 then  
Reaponse.status="404"  
else  
Response.ContentType="application/octet-stream"  
Response.AddHeader "Content-Disposition:","Attachment;filename="&n  
Range=Mid(Request.ServerVariables("HTTP_RANGE"),7)  

if Range="" then  
Response.BinaryWrite(S.Read)  
else  
S.Postion=Clng(Split(Range,"-")(0))  
Response.BinaryWrite(S.Read)  
end if  
End if  
End function  
dim filename  
filename=request("filename")
call download(server.MapPath(filename),filename)  
%> 

二、代码使用方法:

①把下面的代码复制保存为【download.asp】

②然后在下载链接中输入【http://www.xue51.com/download.asp?filename=demo.jpg

当然www.xue51.com要换成你的域名了,demo.jpg就是要下载的jpg文件名。

③文件名必须和download.asp在同一目录

以上就是有关asp实现jpg、txt、html文件直接下载代码的内容,希望对你有所帮助。

转载于:https://www.cnblogs.com/xue51/archive/2012/11/25/2787660.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值