当前位置:软件学堂 > 资讯首页 > 网络编程 > 编程其他 > asp实现jpg、txt、html文件直接下载代码

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

2021/9/11 17:34:37作者:佚名来源:网络

移动端
Dreamweaver (dw)CS5绿色版

大小:119MB语言:

类型:网页制作等级:

今天有网友向学无忧求助,希望解决在asp实现jpg、txt、html文件直接下载的难题,同时希望有详细的asp代码。我们都清楚,如果在网页中下载jpg格式文件会通过IE自动打开的,无法实现点击下载jpg文件,txt、html、asp等文件也是一样IE会自动打开,如何在asp实现jpg、txt、html文件直接下载呢,这个代码又怎么写呢,下面学无忧列出了以下几种程序代码及使用方法:
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文件直接下载代码的相关内容,希望对你有所帮助。

标签: 代码  asp