关闭缓冲后程序出错
这一段程序是将数据导出到EXCEL表中,由于导出的数据太大,所以需要将缓冲关闭,但是我关闭缓冲后就程序出错:
错误信息是:
Response 对象 错误 'ASP 0156 : 80004005 '
HTTP 头错误
/erp/Stock/StockToExcel.asp,行 71
已将 HTTP 头输出到客户端浏览器。任何对 HTTP 头的修改都必须在输出页内容之前进行。
程序错在这四行:
Response.Buffer=True
Response.ContentType= "application/X-msdownload "
Response.Redirect( "report/ " & Request.ServerVariables( "REMOTE_ADDR ") & ".xls ")
源程序如下:
<%
//-----输出的数据量可能会很大导致出错,将缓冲关闭即可-----//
Response.Buffer = False
//-----设置ASP脚本超时为1500秒-----//
Server.ScriptTimeout = 1500
%>
<!--#include file= "../../UserOnLine.asp "-->
<!--#include file= "../Conndb.asp "-->
<!--#include file= "../Functions.asp "-->
<title> 导出结果到Excel表格 </title>
<%
//===赋值为空格,防止在写入不到数据库的首行记录===//
NumGZWH2 = " "
NumP001 = " "
NumQ001 = " "
NumQ002 = " "
NumQ003 = " "
StrCustName = " "
//===保存文件名IP.xls到服务器本目录的report文件夹===//
Set Fs = Server.CreateObject( "scripting.filesystemobject ")
FileName = Server.MapPath( ". ") & "\report\ " & Request.ServerVariables( "REMOTE_ADDR ") & ".xls "
//===文件已存在的话先删除文件===//
if Fs.FileExists(FileName) then
Fs.DeleteFile(FileName)
end if
set MyFile = Fs.CreateTextFile(FileName,true)
//===打开数据库===//
Set Rs = Server.CreateObject( "ADODB.RecordSet ")
Sql = "SELECT DES2 As 客户货号, DES As 货品名称, ITEM_CODE As 货品编号 " _
+ "FROM UN_ITEM1 WHERE " & Session( "CurrentFields ") & " Like ' " & Session( "CustCode ") & "% ' "
Rs.Open Sql,conn,1,1
if Rs.RecordCount> 0 then
//===写标题===//
strLine= " "
strLine = strLine & "客户货号 " & chr(9)
strLine = strLine & "货品名称 " & chr(9)
strLine = strLine & "成品仓 " & chr(9)
strLine = strLine & "包装部 " & chr(9)
strLine = strLine & "退货待处理 " & chr(9)
strLine = strLine & "外发退货 " & chr(9)
strLine = strLine & "坏品区 " & chr(9)
strLine = strLine & "客户名称 " & chr(9)
strLine = strLine & "货品编号 " & chr(9)
MyFile.writeline strLine
//===写内容===//
Do While Not Rs.EOF
Call GetTotalNum()
strLine= " "
strLine = strLine & Rs( "客户货号 ") & chr(9)
strLine = strLine & Rs( "货品名称 ") & chr(9)
strLine = strLine & NumGZWH2 & chr(9)
strLine = strLine & NumP001 & chr(9)
strLine = strLine & NumQ001 & chr(9)
strLine = strLine & NumQ002 & chr(9)
strLine = strLine & NumQ003 & chr(9)
strLine = strLine & StrCustName & chr(9)
strLine = strLine & Rs( "货品编号 ") & chr(9)
MyFile.writeline strLine
Rs.MoveNext
Loop
end if
Rs.Close
set Rs = nothing
conn.close
set conn = nothing
set MyFile = nothing
Set Fs=Nothing
Response.Buffer=True
Response.ContentType= "application/X-msdownload "
Response.Redirect( "report/ " & Request.ServerVariables( "REMOTE_ADDR ") & ".xls ")
%>
[解决办法]
Response.Buffer为False时 你已经输出 <title> 导出结果到Excel表格 </title> 了,只时就是默认输出HTML text/html, 并且已经向客户端输出数据是不能做重定向Redirect的.
[解决办法]
<% 'response.buffer=true%>
<a href= "a "> a </a>
<%response.redirect "request_form.htm "%>
效果:
①.当关闭IIS的缓冲功能,访问该页面时出错
a
答复对象 错误 'ASP 0156 : 80004005 '
头错
/course/response_buffer.asp,行3
HTTP 头已经写入到 客户浏览器。任何 HTTP 头的修改必须在写入页内容之前。
②.当关闭IIS的缓冲功能,去掉文件第一行的注释,则页面重定向成功
③.当打开IIS的缓冲功能,无论是否去掉文件第一行的注释,页面重定向都成功
[解决办法]
缓冲关了就有你这个问题是没办法避免的
你的数据量过大可以在循环里使用
Response.Flush将目前的缓冲输出变相实现缓冲关闭
[解决办法]
Response.Redirect前不能用Response.Flush
用了就规定出错
[解决办法]
Response.Redirect 改成用javascript前台脚本的
Response.write " <script> location.href= 'report/ "&Request.ServerVariables( "REMOTE_ADDR ") & ".xls ' </script> "