當(dāng)前位置:軟件學(xué)堂 > 資訊首頁 > 網(wǎng)絡(luò)編程 > 編程其他 > asp實現(xiàn)jpg、txt、html文件直接下載代碼

asp實現(xiàn)jpg、txt、html文件直接下載代碼

2021/9/11 17:34:37作者:佚名來源:網(wǎng)絡(luò)

移動端
Dreamweaver (dw)CS5綠色版

大?。?19MB語言:

類型:網(wǎng)頁制作等級:

今天有網(wǎng)友向?qū)W無憂求助,希望解決在asp實現(xiàn)jpg、txt、html文件直接下載的難題,同時希望有詳細(xì)的asp代碼。我們都清楚,如果在網(wǎng)頁中下載jpg格式文件會通過IE自動打開的,無法實現(xiàn)點擊下載jpg文件,txt、html、asp等文件也是一樣IE會自動打開,如何在asp實現(xiàn)jpg、txt、html文件直接下載呢,這個代碼又怎么寫呢,下面學(xué)無憂列出了以下幾種程序代碼及使用方法:
asp實現(xiàn)jpg、txt、html文件直接下載代碼

一、程序代碼

asp文件直接下載代碼一:

如果你只需要實現(xiàn)下載jpg、txt、html文件,可以采用這種簡單的代碼,代碼如下:

<%

url=request("filename")

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

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

Response.End() 

%>

 

asp文件直接下載代碼二:

如果你要實現(xiàn)不但可以下載jpg、txt、html格式文件,同時還希望能夠直接下載asp、php等格式文件下載,那么可以用下面的代碼來實現(xiàn),代碼如下:

<%

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

二、代碼使用方法:

①把下面的代碼復(fù)制保存為【download.asp】

②然后在下載鏈接中輸入【http://www.wangbatian.cn/download.asp?filename=demo.jpg

www.wangbatian.cn當(dāng)然要換成你的域名了,demo.jpg就是要下載的jpg文件名。

③文件名必須和download.asp在同一目錄

以上就是有關(guān)asp實現(xiàn)jpg、txt、html文件直接下載代碼的相關(guān)內(nèi)容,希望對你有所幫助。

標(biāo)簽: 代碼  asp