怎么实现文本或html文件的下载方式而不是以IE打开?

漫步彩云之端 2001-11-08 03:57:57
我在网页上放一个超链接。该链接指向一个文本或html页。我想让来访者通过点击该链接下载该文件而不是用IE打开文件进行浏览,请问需要怎么做?
...全文
408 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
漫步彩云之端 2001-11-20
  • 打赏
  • 举报
回复
有人会吗?
luoxxhh 2001-11-20
  • 打赏
  • 举报
回复
试试这个:
实现文件下载而不是由ie打开的代码。
--------- hooke
<%
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\help.gif"
mimeType = "application/x-msdownload"
sucess = TransferFile(path, mimeType,"help.gif")
Response.End
%>

另有我知道两种方法,你试一试吧
一.
<%
fn="thefilenametosave"
Response.AddHeader "Content-Disposition: ","attachment;filename="&fn
%>
<!--#include file="thefilewilldown.txt"-->

二.
Response.ContentType="appliaction/x-txt" '这行用来确定MIME,视数据内容而定
Response.AddHeader "content-disposition", "attachment;filename=纪事.txt"
'这行用于指定用户保存时缺省文件名

'以下是数据
set fs=server.CreateObject("Scripting.FileSystemObject")
cfile=Server.MapPath("../template/"&session("userid")&"/"&filename)
set txtfile=fs.OpenTextFile(cfile)
cTxt=txtfile.ReadAll
Response.Write cTxt
txtfile.close
set fs=nothing
Response.End
dpcdpc 2001-11-08
  • 打赏
  • 举报
回复
好象做不到。除非告诉用户点右键另存为...嘿嘿。
漫步彩云之端 2001-11-08
  • 打赏
  • 举报
回复
怎么没有人回答?

28,391

社区成员

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

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