编程(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文件直接下载代码的相关内容,希望对你有所帮助。 使用编程语言写的程序,由于每条指令都对应计算机一个特定的基本动作,所以程序占用内存少、执行效率高。 |
温馨提示:喜欢本站的话,请收藏一下本站!