我想在一打开某个ASP页面时,就让用户以文本格式下载页面内容,客户端是Win2k正确执行,是XP就不行,为什么???

woshihuzi 2005-11-14 10:06:02
<%
'=====================================
'代码:
'=====================================

' 导出到Txt文件
Sub ExportToTxt

dim resultfile

'得到要保存的文件名
resultfile = "Result.txt"

' 添加文件头信息
Response.ContentType = "txt/html"
Response.addheader "Content-Disposition","attechment;Filename=" & resultfile

' 输出内容
Response.Write "2008奥运吉祥物" & vbcrlf & vbcrlf
response.write "贝贝" & vbcrlf
response.write "晶晶" & vbcrlf
response.write "欢欢" & vbcrlf
response.write "迎迎" & vbcrlf
response.write "妮妮" & vbcrlf

End Sub


Call ExportToTxt


'=========================================================
'win2k下的执行结果(用户可以选择下载,也可以选择在线打开):
'=========================================================
2008奥运吉祥物

贝贝
晶晶
欢欢
迎迎
妮妮


'=========================================================
'XP下的执行结果(用户不可以选择下载,也不可以选择在线打开,网页直接显示在浏览器中):
'=========================================================
2008奥运吉祥物 贝贝晶晶欢欢迎迎妮妮

%>
...全文
149 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jspadmin 2005-11-15
  • 打赏
  • 举报
回复
再附上fso读取文本文件代码:
<%
fname="1.txt"
fpath=Server.MapPath(".")
filepath=fPath & "\" & fname
set fso=server.createobject("scripting.filesystemobject")
set opentxt=fso.opentextfile(filepath)
response.write("以下是1.txt的内容:")
response.write("<br><br>")
'下面相当于response.write(opentxt.readall)
while not opentxt.atendofstream
response.write(opentxt.readline)
response.write("<br>")
wend
opentxt.close
response.write("<hr size=""1"" noshade>")
set opentxt=nothing
set fso=nothing
%>
jspadmin 2005-11-15
  • 打赏
  • 举报
回复
你可以在输出页面先用fso读取文本文件,再response.write 输出,这个你应该会吧?然后提供一个按钮,让用户点击下载,关于强制下载(包括asp文件或htm文件,一般asp或htm文件直接输入连接只会显示出来,不会让你下载源代码的),我给你段代码:
<%
function download(f,n)
'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
Response.end
end function

dim filename
'filename="index.htm"
filename="1.txt"
call download(server.MapPath(filename),filename)
%>
----------------------------------------------------------------------------------
欢迎光临我的小站(提供免费博客申请):http://www.pifoo.com,有问题的话,可以在留言本上给我留言http://www.pifoo.com/guestbook/
woshihuzi 2005-11-15
  • 打赏
  • 举报
回复
老大,这个函数我用测试了一下,只能下载本目录下的文件,它不能正确下载上级目录或者下级目录下的文件。你有没有解决的办法啊?
我的测试文件是这样的:

<%
' 先写上你的函数

' 然后
dim filename,pathname
filename=trim(request("fName"))
pathname=server.MapPath(filename)
if filename<>"" then
call download(pathname,filename)
end if
%>

能下载倒是能下载,但是,我给他的默认下载文件名就不对le
woshihuzi 2005-11-15
  • 打赏
  • 举报
回复
自己顶一下
gameboy766 2005-11-14
  • 打赏
  • 举报
回复
和客户端浏览器设置有关系,和服务器没关系。
skangming 2005-11-14
  • 打赏
  • 举报
回复
客户端用2K能正常访问,用XP不能正常访问

还是服务器用2K可以正常访问,用XP不行?
woshihuzi 2005-11-14
  • 打赏
  • 举报
回复
自己顶一下,期待高手给与解答。谢谢!
woshihuzi 2005-11-14
  • 打赏
  • 举报
回复
测试代码如下,我想让用户一打开这个页面,用户浏览器上就会出现一个对话框,让用户选择是直接打开还是下载txt文件。

<%
' 导出到Txt文件

' 添加文件头信息
Response.ContentType = "application/octet-stream"
Response.addheader "Content-Disposition","attechment;Filename=result.txt"

' 输出内容
Response.Write "2008奥运吉祥物" & vbcrlf & vbcrlf
response.write "贝贝" & vbcrlf
response.write "晶晶" & vbcrlf
response.write "欢欢" & vbcrlf
response.write "迎迎" & vbcrlf
response.write "妮妮" & vbcrlf

%>
woshihuzi 2005-11-14
  • 打赏
  • 举报
回复
这样好是好了,达到了世界大同,不再因为客户端操作系统是XP而导致不能换行了.

可是无论客户端操作系统是2k还是xp,都是用浏览器直接打开我服务器上的页面,,用户想保存的话,还要点击"文件"菜单的"另存为"按钮.很不方便,难道就没有一个好的法子,让用户想保存就保存,想直接打开就直接打开?
yefeng119 2005-11-14
  • 打赏
  • 举报
回复
用流吧stream,不就好了!
lisoon 2005-11-14
  • 打赏
  • 举报
回复
Response.ContentType = "txt/html"'txt/html//改成application/octet-stream
woshihuzi 2005-11-14
  • 打赏
  • 举报
回复
是客户端.
如果客户端是Win2k,打开页面的时候,会弹出一个对话框,让用户选择"下载"按钮还是"打开"按钮.如果选择下载,则以文本格式保存到客户端机器上,如果选择"打开",则会调用客户端的记事本打开文件.

如果客户端是VP的话,那么,打开网页的时候,根本不会弹出一个对话框,它就直接用网页浏览器打开。由于浏览器不解释VBcrlf,而把<br>解释为换行。这样,就会出现我上面说的问题。

我现在想请教大家的是:
该如何编写代码,才能保证不管客户端的操作系统是2k还是XP,用户都能下载文本格式的文件,不想用客户端的浏览器打开直接。

28,406

社区成员

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

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