ASP读取文本文件并按行或内容大小分页(ASP分页读取文本文件)


    第一种方法:把文件内容行用回车(VbCrLf)分隔为数组,翻时显示数组的一部分,优点是内容显示完整行,但大文件时速度会慢


<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312'></head>
<body>
<%
starttime = timer()
'以下为打开httpd.ini 并分页显示内容
pages = 0

set fs=server.createobject("scripting.filesystemobject")
file=server.mappath("httpd.ini")
set txt=fs.opentextfile(file,1,true)

if not txt.atendofstream then

line = txt.ReadAll
line_ary = split(line,VbCrLf) '把打开内容用回车分成数级以便分组

end if
txt.close
set txt = nothing
set fs = nothing

num = ubound(line_ary)  '总行数

perpage = 6  '每页显示的行数
curr_page = request("page") '当前页
setpages = 10 '10行翻一屏

 '以下为处理当前页
    if curr_page = "" then
       curr_page = 1
    else
        curr_page = clng(curr_page)
        if curr_page<=0 then curr_page = 1
    end if
'处理当前页结束


if num > perpage then   '大于1页时
    page = setpages + 1
    offset = Ceil(setpages/2-1)
    pages = Ceil(num / perpage) '总页数
  
    if pages>1 and curr_page > pages then curr_page = pages
   

    from = curr_page - offset
    to_end = curr_page + offset
    more = 0

if page >= pages then
   from = 2
   to_end = pages-1
else   
    if from <= 1 then
        to_end = page-1
        from = 2
    elseif to_end >= pages then
        from = pages-(page-2)
        to_end = pages-1
    end if   
    more = 1   
   
end if   

    '以下为输出翻页

multipage = "共"&num&"行&nbsp;&nbsp;每页 "& perpage &" 行"

if curr_page > 0 then
    multipage = multipage & "<a href='?page=" & curr_page-1 & "'>上一页</a>" & VbCrLf
    if curr_page = 1 then
        multipage = multipage & "<span>1</span>" & VbCrLf
    elseif curr_page > 6 and more then
        multipage = multipage & "<a href='?page=1'>1</a>..." & VbCrLf
    else
        multipage = multipage & "<a href='?page=1'>1</a>" & VbCrLf
    end if
end if       
   
for i = from to to_end
    if i=curr_page then
        multipage = multipage & "<span>"& i &"</span>" & VbCrLf       
    else
        multipage = multipage & "<a href='?page="&i&"'>"&i&"</a>" & VbCrLf
    end if
next

if curr_page < pages then
    if curr_page < pages-5 and more then
        multipage = multipage & "...<a href='?page="&pages&"'>"&pages&"</a>" & VbCrLf
        multipage = multipage & "<a href='?page=" & curr_page+1 & "'>下一页</a>" & VbCrLf
    else
        multipage = multipage & "<a href='?page="&pages&"'>"&pages&"</a>" & VbCrLf
        multipage = multipage & "<a href='?page=" & curr_page+1 & "'>下一页</a>" & VbCrLf
   
    end if
elseif curr_page = pages then
        multipage = multipage & "<span>"& pages &"</span>" & VbCrLf   
        multipage = multipage & "<a href='?page=" & curr_page & "'>下一页</a>" & VbCrLf    
    else
        multipage = multipage & "<a href='?page="&pages&"'>"&pages&"</a>" & VbCrLf
        multipage = multipage & "<a href='?page=" & curr_page+1 & "'>下一页</a>" & VbCrLf    
end if

   'response.Write multipage

end if

'以下为输出当前页每行
if curr_page = 1 then
    start = 0
    end_is = perpage
else
    start = perpage * (curr_page-1)
    end_is = perpage * curr_page
end if

if curr_page = pages or pages=0 then  end_is = Ubound(line_ary)

for i = start to end_is
  response.Write line_ary(i) &"<br>"
next
'输出结束

'输出分页
if pages>0 then response.Write "<br><br>" & multipage  


%>
<center>Protime:<%=(timer - starttime)*1000%>ms</center>

</body></html>

<%
Function Ceil(inNum)
    Dim nNum, tempInt
    nNum = Fix(inNum)
If inNum > nNum Then
tempInt = nNum + 1
Else
 tempInt = nNum
End If
Ceil = tempInt
End Function
%>



    第二种方法:按一定字节大小分页,这种方法优点是速度比较快,但不足是把内容截断了

<%
'*********************************
'代码及文档由NB联盟成员DLL编写
'您可以自由使用此类,但请适当保留版权信息
'2004-4-14最后修改
'*********************************
Function Bytes2bStr(vin)
if lenb(vin) =0 then
Bytes2bStr = ""
exit function
end if
''二进制转换为字符串
Dim BytesStream,StringReturn
Set BytesStream = Server.CreateObject("ADODB.Stream")
BytesStream.Type = 2
BytesStream.Open
BytesStream.WriteText vin
BytesStream.Position = 0
BytesStream.Charset = "gb2312"
BytesStream.Position = 2
StringReturn = BytesStream.ReadText
BytesStream.close
Set BytesStream = Nothing
Bytes2bStr = StringReturn
End Function


Dim F,File,starttime,PageSize,Page,TotalLength,TotalPage,Content,Position_S
starttime = timer()
PageSize = 500
Page = Request("Page")
If Page = "" Or Not IsNumeric(Page) Then Page = 1
Page = Cint(Page)
If Page < 1 Then Page = 1

'Set F = SERVER.CREATEOBJECT("SCRIPTING.FILESYSTEMOBJECT")
'Set File = F.OPENTEXTFILE(SERVER.MAPPATH("A.TXT"),1)

set s = server.createobject("adodb.stream")
s.mode = 3
s.type = 2
s.open
s.loadfromfile(SERVER.MAPPATH("httpd.ini"))

TotalLength = s.size
TotalPage = TotalLength \ PageSize + 1
If Page > TotalPage Then Page = TotalPage
Position_S = PageSize * (Page - 1)
Position_E = PageSize * Page
If Position_S < 0 Then Position_S = 0
If Position_E > TotalLength Then Position_E = TotalLength

s.Position = Position_S
Content = Replace(Replace(Bytes2bStr(s.ReadText(Position_E - Position_S)),VBcrlf,"<br>")," "," ")


Response.Write "<title>读取文本文件分页示例</title><meta http-equiv='Content-Type' content='text/html; charset=gb2312'><font style='font-size:12px'>"
Response.Write "文件大小:<font color=red>" & TotalLength & "</font>字节 每页<font color=red>" & PageSize & "</font>字节 共<font color=red>" & TotalPage & "</font>页 当前第<font color=red>" & Page & "</font>页 <a href=?Page="& Page - 1 &">上页</a> <a href=?Page="& Page + 1 &">下页</a>"
Response.write "<table width=90% border=0 bgcolor=#999999 cellpadding=3 cellspacing=1 style='font-size:12px;TABLE-LAYOUT:fixed;word-break:break-all'><tr><td bgcolor=#FFFFFF><b>文件内容</b></td></tr><tr><td bgcolor=#F6F6F6>"& Content &"</td></tr></table>"
Response.write "<br><br><br>Protime:" & (timer - starttime)*1000 & "ms</font>"
%>
本博客所有文章如无特别注明均为原创。作者:sysdee复制或转载请以超链接形式注明转自 成功志
原文地址《ASP读取文本文件并按行或内容大小分页(ASP分页读取文本文件)
分享到:更多

相关推荐

发表评论

路人甲 表情
看不清楚?点图切换 Ctrl+Enter快速提交

网友评论(0)