当前位置导航:炫浪网>>网络学院>>在线图书>>网页制作教程>>ASP入门到精通>>第七章 asp编程实例

7.6 用文本+ASP打造新闻发布系统

 

//图片上传


//新闻添加







新闻发布系统

























新闻发布系统后台管理--新闻添加
新闻标题
新闻内容
新闻来源
图片上传











'###################
news_input.asp

<%
'Fields("xxx").Name 取得Form中xxx(Form Object)的名字
'Fields("xxx").FilePath 如果是file Object 取得文件的完整路径
'Fields("xxx").FileName 如果是file Object 取得文件名
'Fields("xxx").ContentType 如果是file Object 取得文件的类型
'Fields("xxx").Length 取得Form中xxx(Form Object)的数据长度
'Fields("xxx").Value 取得Form中xxx(Form Object)的数据内容
Dim FormData,FormSize,gnote,bnote,notes,binlen,binstr
FormSize=Request.TotalBytes
FormData=Request.BinaryRead(FormSize)
Set Fields = GetUpload(FormData)

'############判断输入错误
dim news_title,news_content,news_src,mysession

mysession=Fields("mysession").value
if len(mysession)=0 then
Response.Write "非法登陆或超时请重新登陆"
Response.End
end if

news_title=Fields("news_title").value
news_title=replace(news_title,"|","|")
news_content=Fields("news_content").value
news_src=Fields("news_src").value
news_src=replace(news_src,"|","|")
if len(news_title)=0 then%>

<%Response.end
end if

if len(news_content)=0 then%>

<%end if

if len(news_src)=0 then%>

<%Response.end
end if

dim varchar
varchar=right(Fields("server1").value,3)
if len(varchar)<>0 then
if varchar<>"gif" and varchar<>"jpg" then
%>

<% Response.end
else
end if
end if
'###########将图片写入文件夹

set file_O=Server.CreateObject("Scripting.FileSystemObject")

'##########当前时间做图片名
dim newname,mytime,newfile,filename,id,image
endname=right(fields("server1").value,4)
mytime=now()
id=Year(mytime)&Month(mytime)&Day(mytime)&Hour(mytime)&Minute(MyTime)&Second(MyTime)
imageid=id&endname

'#############写入图片
newfile="client1"
filename=Fields("server1").value

If Fields(newfile).FileName<>"" Then
file_name=Server.MapPath("./images/"&imageid&"")
set outstream=file_O.CreateTextFile(file_name,true,false)
binstr=Fields(newfile).Value
binlen=1
varlen=lenb(binstr)
for i=1 to varlen
clow = MidB(binstr,i,1)
If AscB(clow) = 255 then
outstream.write chr(255)
binlen=binlen+1
if (i mod 2)=0 then
notes=gnote
exit for
end if
elseif AscB(clow) > 128 then
clow1=MidB(binstr,i+1,1)
if AscB(clow1) <64 or AscB(clow1) =127 or AscB(clow1) = 255 then
binlen=binlen+1
'if (binlen mod 2)=0 then
binlen=binlen+1
outstream.write Chr(AscW(ChrB(128)&clow))
'end if
notes=bnote
exit for
else
outstream.write Chr(AscW(clow1&clow))
binlen=binlen+2
i=i+1
if (i mod 2)=0 then
notes=gnote
exit for
end if
end if
else
outstream.write chr(AscB(clow))
binlen=binlen+1
if (i mod 2)=0 then
notes=gnote
exit for
end if
end if
next
outstream.close
set outstream=file_O.OpenTextFile(file_name,8,false,-1)
outstream.write midb(Fields(newfile).Value,binlen)
outstream.close
if notes=bnote then notes=notes&(binlen-1)&"字节处。"

End If

'###################################################################################### 把新闻数据结构写入newslist文件
dim mappath,mytext,myfso,contenttext,news_addtime,news_point
news_point=1
news_addtime=mytime
set myfso=createobject("scripting.filesystemobject")
mappath=server.mappath("./")

set mytext=myfso.opentextfile(mappath&"\new_list.asp",8,-1)

dim mytext2
if len(varchar)<>0 then
mytext2=trim(id&","&news_title&","&id&".txt"&","&news_src&","&news_point&","&news_addtime&","&imageid&"|")
else
mytext2=trim(id&","&news_title&","&id&".txt"&","&news_src&","&news_point&","&news_addtime&"|")
end if
mytext.writeline(mytext2)
mytext.close

'##############把新闻内容写入相应的文件中
set contenttext=myfso.OpenTextFile(mappath&"\news_content\"&id&".txt",8,-1)
function htmlencode2(str) '#############字符处理函数
dim result
dim l
l=len(str)
result=""
dim i
for i = 1 to l
select case mid(str,i,1)
case chr(34)
result=result+"''"
case "&"
result=result+"&"
case chr(13)
result=result+"
"
case " "
result=result+" "
case chr(9)
result=result+" "
case chr(32)
if i+1<=l and i-1>0 then
if mid(str,i+1,1)=chr(32) or mid(str,i+1,1)=chr(9) or mid(str,i-1,1)=chr(32) or mid(str,i-1,1)=chr(9) then
result=result+" "
else
result=result+" "
end if
else
result=result+" "
end if
case else
result=result+mid(str,i,1)
end select
next
htmlencode2=result
end function
'############################################################################

contenttext.write htmlencode2(news_content)
contenttext.close
set myfso=nothing
%>

//新闻列表显示
<%
dim myfso,myread
set myfso=createobject("scripting.filesystemobject")
set myread=myfso.opentextfile(server.mappath("./new_list.asp"),1,0)

if myread.atendofstream then
Response.Write "目前没有添加新闻"
Response.End
else

dim mytext,listarray
mytext=myread.readall
listarray=split(mytext,"|") '#######把所有记录分割成一个数组a
dim recordcount,pagecount, pagesize, pagenum
recordcount=ubound(listarray)'############记录条数
pagesize=2
pagecount=recordcount/pagesize '#######取得页面数
if instr(1,pagecount,".")=null or instr(1,pagecount,".")=0 then
pagenum=pagecount
else
pagenum=int(pagecount)+1
end if

dim topage
topage=cint(Request.QueryString ("topage")) '########取得要显示的页面
if topage<=0 then
topage=1
end if
if topage>pagenum then
topage=pagenum
end if


dim i,j,n
b=listarray
for i=0 to recordcount-1 '########把每一条记录组成一个数组
j=split(listarray(i),",")
if ubound(j)=6 then
b(i)="
  • " & j(1) & "(图) 点击:" & j(4)&"次 最后发布时间:"&j(5)&""
    else
    b(i)="
  • " & j(1) & " 点击:" & j(4)&"次 最后发布时间:"&j(5)&""
    end if
    next

    '########把记录反排序存储在新的数组实现按时间反排序
    dim c(100)
    n=0
    for i=recordcount to 0 step -1
    c(n)=b(i)
    n=n+1
    next


    dim currentrecord
    currentrecord=pagesize*(topage-1)+1 '#########显示每一页
    for k=1 to pagesize
    if len(c(currentrecord))=0 then
    exit for
    end if
    Response.Write c(currentrecord)&"
    "
    currentrecord=currentrecord+1
    next
    Response.Write ""
    for m=1 to pagenum
    response.write ""&m&" "
    next

    end if%>
    //新闻删除

    <%
    dim id
    id=Request.QueryString ("id")
    dim myfso
    set myfso=createobject("scripting.filesystemobject")
    if myfso.FileExists(server.mappath("./news_content/"&id&".txt"))then
    myfso.DeleteFile (server.mappath("./news_content/"&id&".txt"))'#############删除新闻内容
    end if

    dim mytext2,myread2
    set myread2=myfso.opentextfile(server.mappath("./new_list.asp"),1,0)
    if myread2.atendofstream then
    Response.Write "没有新闻内容"
    myread2.close
    Response.End
    end if
    mytext2=myread2.readall
    myread2.close
    dim listarray,i,h,count,sf,title
    listarray=split(mytext2,"|") '#########读取记录并以#分割成listarray数组
    count=ubound(listarray)
    for i=0 to count '###########根据ID找到该新闻实现删除功能
    sf=split(listarray(i),",")
    if right(sf(0),7)=right(id,7) then
    dim thisid
    thisid=i

    '#######为6说明上传了图片,删除新闻图片和该列表记录
    if ubound(sf)=6 then
    myfso.deletefile(server.MapPath ("./images/"&sf(6)))
    end if
    exit for
    end if
    next

    dim mytext,mappath
    mappath=server.mappath("./")
    set mytext=myfso.createtextfile(mappath&"\new_list.asp",-1,0)
    for i=0 to thisid-1' ##########把所有数据重新写入文件
    mytext.write listarray(i)&"|"
    next

    for i=thisid+1 to ubound(listarray)
    if i=ubound(listarray) then
    mytext.write listarray(i)
    exit for
    else
    mytext.write listarray(i)&"|"
    end if
    next
    mytext.close
    %>

    ---------------
    news_view.asp
    <% Response.Expires=0
    dim myid,myfso,myread,mytext1
    myid=request.querystring("id")

    if len(myid)=0 then
    Response.Write "没有该新闻"
    Response.End
    end if

    set myfso=createobject("scripting.filesystemobject")
    set myread=myfso.opentextfile(server.mappath("./news_content/"&myid&".txt"),1,0)
    if myread.atendofstream then
    Response.Write "没有新闻内容"
    Response.End
    else
    mytext1=myread.readall '#######打开对应的新闻内容文件,并读取用变量存储


    function htmlencode2(str)'###########字符处理函数
    dim result
    dim l
    l=len(str)
    result=""
    dim i
    for i = 1 to l
    select case mid(str,i,1)
    case chr(34)
    result=result+""""
    case "&"
    result=result+"&"
    case chr(13)
    result=result+"
    "
    case " "
    result=result+" "
    case chr(9)
    result=result+" "
    case chr(32)
    result=result+" "
    if i+1<=l and i-1>0 then
    if mid(str,i+1,1)=chr(32) or mid(str,i+1,1)=chr(9) or mid(str,i-1,1)=chr(32) or mid(str,i-1,1)=chr(9) then
    result=result+" "
    else
    result=result+" "
    end if
    else
    result=result+" "
    end if
    case else
    result=result+mid(str,i,1)
    end select
    next
    htmlencode2=result
    end function


    myread.close
    end if

    dim mytext2,myread2
    set myread2=myfso.opentextfile(server.mappath("./new_list.asp"),1,0)

    if myread2.atendofstream then
    Response.Write "没有新闻内容"
    Response.End
    else
    mytext2=myread2.readall
    myread2.close
    dim listarray,i,h
    listarray=split(mytext2,"|") '#########读取记录并以#分割成listarray数组

    dim count,sf,title,src
    count=ubound(listarray)

    for i=0 to count '###########根据ID找到该新闻并把文章点击次数加1
    sf=split(listarray(i),",")
    if right(sf(0),7)=right(myid,7) then
    title=sf(1)
    src=sf(3)
    sf(4)=sf(4)+1

    '#######为6说明上传了图片,存储为新的数组
    if ubound(sf)=6 then
    listarray(i)=sf(0)&","&sf(1)&","&sf(2)&","&sf(3)&","&sf(4)&","&sf(5)&","&sf(6)
    dim mypic
    mypic=sf(6)
    else
    listarray(i)=sf(0)&","&sf(1)&","&sf(2)&","&sf(3)&","&sf(4)&","&sf(5)
    end if
    '##################
    exit for
    end if
    next

    dim k,mytext,mappath
    mappath=server.mappath("./")
    set mytext=myfso.createtextfile(mappath&"\new_list.asp",-1,0)
    for i=0 to ubound(listarray)' ##########把所有数据重新写入文件
    if i=ubound(listarray) then
    mytext.write listarray(i)
    else
    mytext.write listarray(i)&"|"
    end if
    next
    Response.Write ""
    Response.Write"
    "&title&"

    "
    Response.Write "
    "
    if len(mypic)<>0 then
    Response.write "
    "
    end if
    Response.Write ""&htmlencode2(mytext1)&""
    Response.Write "
    新闻来源:"&src&"
    "
    %>






    <% end if%>
    //新闻修改
    ‘#######news_update.asp


    <% dim myid
    myid=Request.QueryString ("id")
    if len(myid)=0 then
    Response.Write "没有该新闻"
    Response.End
    end if
    dim myfso,myread,mytext,newscontent
    '#######打开对应的新闻内容文件,并读取用变量存储
    set myfso=createobject("scripting.filesystemobject")
    if myfso.FileExists (server.mappath("./news_content/"&myid&".txt")) then
    set myread=myfso.opentextfile(server.mappath("./news_content/"&myid&".txt"),1,0)
    newscontent=myread.readall
    myread.close
    newscontent=replace(newscontent,"
    ",chr(13))
    newscontent=replace(newscontent," "," ")
    newscontent=replace(newscontent," ",chr(32))
    newscontent=replace(newscontent,"'' ",chr(34))
    else
    Response.Write "该新闻已被删除"
    Response.End
    end if
    dim mytext2,myread2 '#######打开新闻列表文件
    set myread2=myfso.opentextfile(server.mappath("./new_list.asp"),1,0)
    if myread2.atendofstream then
    Response.Write "没有新闻内容"
    Response.End
    end if
    mytext2=myread2.readall
    dim listarray
    listarray=split(mytext2,"|") '#########读取记录并以#分割成listarray数组
    dim count,sf,i,title,src
    count=ubound(listarray)
    for i=0 to count '###########根据ID找到该新闻并用变量存储给新闻的标题
    sf=split(listarray(i),",")
    if right(sf(0),7)=right(myid,7) then
    title=sf(1)
    src=sf(3)
    exit for
    end if
    next
    %>


























    新闻发布系统后台管理--新闻修改
    新闻标题
    " class="buttonface2 ">











    新闻来源
    " size="93" class="buttonface2 ">
    图片上传







    ">



    ##########
    news_updating.asp


    <%
    'Fields("xxx").Name 取得Form中xxx(Form Object)的名字
    'Fields("xxx").FilePath 如果是file Object 取得文件的完整路径
    'Fields("xxx").FileName 如果是file Object 取得文件名
    'Fields("xxx").ContentType 如果是file Object 取得文件的类型
    'Fields("xxx").Length 取得Form中xxx(Form Object)的数据长度
    'Fields("xxx").Value 取得Form中xxx(Form Object)的数据内容
    Dim FormData,FormSize,gnote,bnote,notes,binlen,binstr
    FormSize=Request.TotalBytes
    FormData=Request.BinaryRead(FormSize)
    Set Fields = GetUpload(FormData)
    '############判断输入错误
    dim mytitle,content,src,id,mysession
    mysession=Fields("newtitle").value
    if len(mysession)=0 then
    Response.Write "非法登陆或超时间,请重新登陆"
    Response.End
    end if
    mytitle=Fields("newtitle").value
    mytitle=replace(mytitle,"|","|")
    mytitle=replace(mytitle,"
    ","")
    content=Fields("newcontent").value
    src=Fields("newssrc").value
    src=replace(src,"|","|")
    src=replace(src,"
    ","")
    id=trim(right(Fields("myid").value,12))
    if len(mytitle)=0 then
    Response.Write ""
    end if
    if len(content)=0 then
    Response.Write ""
    end if
    if len(src)=0 then
    Response.Write ""
    end if

    '############################################################################################图片更该功能的实现
    newfile="client1"
    If Fields(newfile).FileName<>"" Then
    set file_0=Server.CreateObject("Scripting.FileSystemObject")
    dim contextname
    contextname=right(Fields("client1").FileName,4)
    imageid=id&contextname
    if contextname<>".gif?http://www.xvna.com" and contextname<>".jpg?http://www.xvna.com" then '#########判断上传文件格式
    Response.Write ""
    end if
    file_name=Server.MapPath("./images/"&imageid&"")
    '#####################################如果原来有图片文件主名为id的则删除该图片
    if file_0.fileexists(server.MapPath ("./images/"&id&".gif?http://www.xvna.com")) then
    Set f3 = file_0.GetFile(server.MapPath ("./images/"&id&".gif?http://www.xvna.com"))
    f3.Delete
    end if
    if file_0.fileexists(server.MapPath ("./images/"&id&".jpg?http://www.xvna.com")) then
    Set f3 = file_0.GetFile(server.MapPath ("./images/"&id&".jpg?http://www.xvna.com"))
    f3.Delete
    end if
    '########################################写入图片
    set outstream=file_0.openTextFile(file_name,8,-1)
    binstr=Fields("client1").Value
    binlen=1
    varlen=lenb(binstr)
    for i=1 to varlen
    clow = MidB(binstr,i,1)
    If AscB(clow) = 255 then
    outstream.write chr(255)
    binlen=binlen+1
    if (i mod 2)=0 then
    notes=gnote
    exit for
    end if
    elseif AscB(clow) > 128 then
    clow1=MidB(binstr,i+1,1)
    if AscB(clow1) <64 or AscB(clow1) =127 or AscB(clow1) = 255 then
    binlen=binlen+1
    'if (binlen mod 2)=0 then
    binlen=binlen+1
    outstream.write Chr(AscW(ChrB(128)&clow))
    'end if
    notes=bnote
    exit for
    else
    outstream.write Chr(AscW(clow1&clow))
    binlen=binlen+2
    i=i+1
    if (i mod 2)=0 then
    notes=gnote
    exit for
    end if
    end if
    else
    outstream.write chr(AscB(clow))
    binlen=binlen+1
    if (i mod 2)=0 then
    notes=gnote
    exit for
    end if
    end if
    next
    outstream.close
    set outstream=file_0.OpenTextFile(file_name,8,false,-1)
    outstream.write midb(Fields(newfile).Value,binlen)
    outstream.close
    if notes=bnote then notes=notes&(binlen-1)&"字节处。"
    End If
    '#######################################################################################################
    dim myfso,mywrite '#######修改新闻详细内容
    set myfso=createobject("scripting.filesystemobject")
    if myfso.FileExists(server.mappath("./news_content/"&id&".txt")) then
    myfso.DeleteFile (server.mappath("./news_content/"&id&".txt"))
    end if
    set mywrite=myfso.createtextfile(server.mappath("./news_content/"&id&".txt"),-1,0)
    mywrite.write content

    dim mytext2,myread2 '#########修改新闻的标题来源
    set myread2=myfso.opentextfile(server.mappath("./new_list.asp"),1,0)
    mytext2=myread2.readall
    dim listarray,i,h,count,sf
    listarray=split(mytext2,"|") '#########读取记录并以#分割成listarray数组
    count=ubound(listarray)
    for i=0 to count '###########根据ID找到该新闻记录
    sf=split(listarray(i),",")
    if right(sf(0),7)=right(id,7) then
    sf(1)=mytitle
    sf(3)=src
    '#######为6说明上传了图片,存储新的数组实现查看记录点击次数加1
    if ubound(sf)=6 then
    If Fields(newfile).FileName<>"" Then
    sf(6)=imageid
    end if
    listarray(i)=sf(0)&","&sf(1)&","&sf(2)&","&sf(3)&","&sf(4)&","&sf(5)&","&sf(6)
    else
    listarray(i)=sf(0)&","&sf(1)&","&sf(2)&","&sf(3)&","&sf(4)&","&sf(5)
    end if
    '##################
    exit for
    end if
    next

    function htmlencode2(str) '#############字符处理函数
    dim result
    dim l
    l=len(str)
    result=""
    dim i
    for i = 1 to l
    select case mid(str,i,1)
    case chr(34)
    result=result+"''"
    case "&"
    result=result+"&"
    case chr(13)
    result=result+"
    "
    case " "
    result=result+" "
    case chr(9)
    result=result+" "
    case chr(32)
    if i+1<=l and i-1>0 then
    if mid(str,i+1,1)=chr(32) or mid(str,i+1,1)=chr(9) or mid(str,i-1,1)=chr(32) or mid(str,i-1,1)=chr(9) then
    result=result+" "
    else
    result=result+" "
    end if
    else
    result=result+" "
    end if
    case else
    result=result+mid(str,i,1)
    end select
    next
    htmlencode2=result
    end function
    '##########################
    dim k,mytext,mappath
    mappath=server.mappath("./")
    set mytext=myfso.createtextfile(mappath&"\new_list.asp",-1,0)
    for i=0 to ubound(listarray)' ##########把所有数据重新写入文件
    if i=ubound(listarray) then
    mytext.write htmlencode2(listarray(i))
    else
    mytext.write htmlencode2(listarray(i)&"|")
    end if
    next
    %>
  • 相关内容
    赞助商链接