如何让指向文本文件的联接下载而不是打开,希望大虾门能提供asp代码

usstdiy 2003-04-04 05:31:09
谢谢~
...全文
30 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
laker_tmj 2003-04-10
  • 打赏
  • 举报
回复
up
伟大de虫子 2003-04-04
  • 打赏
  • 举报
回复
以下是解决方案,能下载任何文件(包括asp之类,只要你不限制):

引用 <a href=download.asp?file=文件的相对路径>下载</a>

download.asp
---------------------------------------------------------------------------
<%
call downloadFile(replace(replace(Request("file"),"\",""),"/",""))

Function downloadFile(strFile)

strFilename = server.MapPath(strFile)

Response.Buffer = True
Response.Clear

Set s = Server.CreateObject("ADODB.Stream")
s.Open

s.Type = 1

on error resume next


'检查文件是否存在,本程序QQ44457690简单男人 搜集
Set fso = Server.CreateObject("Scripting.FileSystemObject")
if not fso.FileExists(strFilename) then
Response.Write("<h1>错误</h1>" & strFilename & " 不存在<p>")
Response.End
end if

' 获得文件大小
Set f = fso.GetFile(strFilename)
intFilelength = f.size


s.LoadFromFile(strFilename)
if err then
Response.Write("<h1>错误 </h1>" & err.Description & "<p>")
Response.End
end if

' send the headers to the users browser
Response.AddHeader "Content-Disposition", "attachment; filename=" & f.name
Response.AddHeader "Content-Length", intFilelength
Response.CharSet = "UTF-8"
Response.ContentType = "application/octet-stream"

' output the file to the browser
Response.BinaryWrite s.Read
Response.Flush


' tidy up
s.Close
Set s = Nothing


End Function

%>



另一个例子:
----------------------------------------------------------------------
<%
Const ForReading=1
Const TristateTrue=-1 'Unicode
Const FILE_TRANSFER_SIZE=16384 '16k

'Use the following line for IIS4/PWS - this is the default for IIS5
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
'Server.MapPath(path)
path = "C:\Inetpub\wwwroot\prod\images\bar.gif"
mimeType = "application/x-msdownload"
sucess = TransferFile(path, mimeType,"bar.gif")
Response.End
%>

quiet 2003-04-04
  • 打赏
  • 举报
回复
回复人: tdl982324(石井坚) ( ) 信誉:143 2003-01-08 10:48:00 得分:0


<%
下载文件Function
Function downloadFile(strFile)
strFilename = server.MapPath(strFile)
Response.Buffer = True
Response.Clear
Set s = Server.CreateObject("ADODB.Stream")
s.Open
s.Type = 1
on error resume next
Set fso = Server.CreateObject("Scripting.FileSystemObject")
if not fso.FileExists(strFilename) then
Response.Write("<h1>Error:</h1>" & strFilename & " does not exist<p>")
Response.End
end if
Set f = fso.GetFile(strFilename)
intFilelength = f.size
s.LoadFromFile(strFilename)
if err then
Response.Write("<h1>Error: </h1>" & err.Description & "<p>")
Response.End
end if
Response.AddHeader "Content-Disposition", "attachment; filename=" &f.name
Response.AddHeader "Content-Length", intFilelength
Response.CharSet = "UTF-8"
Response.ContentType = "application/octet-stream"
Response.BinaryWrite s.Read
Response.Flush
s.Close
Set s = Nothing
End Function

downloadFile("*.txt") '中间写文件虚拟路径
%>
妈的,给分,我受够了
par 2003-04-04
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/1391/1391669.xml?temp=.5950739

http://expert.csdn.net/Expert/topic/1294/1294203.xml?temp=.8195612

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧