編程(Programming)是編定程序的中文簡稱,就是讓計算機代碼解決某個問題,對某個計算體系規定一定的運算方式,使計算體系按照該計算方式運行,并最終得到相應結果的過程。為了使計算機能夠理解(understand)人的意圖,人類就必須將需解決的問題的思路、方法和手段通過計算機能夠理解的形式告訴計算機,使得計算機能夠根據人的指令一步一步去工作,完成某種特定的任務。這種人和計算體系之間交流的過程就是編程。 今天有網友向本站求助,希望解決在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文件直接下載代碼的相關內容,希望對你有所幫助。 使用編程語言寫的程序,由于每條指令都對應計算機一個特定的基本動作,所以程序占用內存少、執行效率高。 |
溫馨提示:喜歡本站的話,請收藏一下本站!