asp編程執行個體:用文本+ASP打造新聞發布系統1

來源:互聯網
上載者:User

標籤:style   http   io   ar   color   os   sp   java   for   

  //圖片上傳
  〈SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT〉
  Function GetUpload(FormData)
  Dim DataStart,DivStr,DivLen,DataSize,FormFieldData
  ‘分隔標誌串(+CRLF)
  DivStr = LeftB(FormData,InStrB(FormData,str2bin(VbCrLf)) + 1)
  ‘分隔標誌串長度
  DivLen = LenB(DivStr)
  PosOpenBoundary = InStrB(FormData,DivStr)
  PosCloseBoundary = InStrB(PosOpenBoundary + 1,FormData,DivStr)
  Set Fields = CreateObject("Scripting.Dictionary")
  While PosOpenBoundary 〉 0 And PosCloseBoundary 〉 0
  ‘name起始位置(name="xxxxx"),加6是因為[name="]長度為6
  FieldNameStart = InStrB(PosOpenBoundary,FormData,str2bin("name=")) + 6
  FieldNameSize = InStrB(FieldNameStart,FormData,ChrB(34)) - FieldNameStart ‘(")的ASC值=34
  FormFieldName = bin2str(MidB(FormData,FieldNameStart,FieldNameSize))
  ‘filename起始位置(filename="xxxxx")
  FieldFileNameStart = InStrB(PosOpenBoundary,FormData,str2bin("filename=")) + 10
  If FieldFileNameStart 〈 PosCloseBoundary And FieldFileNameStart 〉 PosopenBoundary Then
  FieldFileNameSize = InStrB(FieldFileNameStart,FormData,ChrB(34)) - FieldFileNameStart ‘(")的ASC值=34
  FormFileName = bin2str(MidB(FormData,FieldFileNameStart,FieldFileNameSize))
  Else
  FormFileName = ""
  End If
  ‘Content-Type起始位置(Content-Type: xxxxx)
  FieldFileCTStart = InStrB(PosOpenBoundary,FormData,str2bin("Content-Type:")) + 14
  If FieldFileCTStart 〈 PosCloseBoundary And FieldFileCTStart 〉 PosOpenBoundary Then
  FieldFileCTSize = InStrB(FieldFileCTStart,FormData,str2bin(VbCrLf & VbCrLf)) - FieldFileCTStart
  FormFileCT = bin2str(MidB(FormData,FieldFileCTStart,FieldFileCTSize))
  Else
  FormFileCT = ""
  End If
  ‘資料起始位置:2個CRLF開始
  DataStart = InStrB(PosOpenBoundary,FormData,str2bin(VbCrLf & VbCrLf)) + 4
  If FormFileName 〈〉 "" Then
  ‘資料長度,減1是因為資料檔案的存取位元組數問題(可能是AppendChunk方法的問題):
  ‘由於位元組數為奇數的圖象存到資料庫時會去掉最後一個字元導致圖象不能正確顯示,
  ‘位元組數為偶數的資料檔案就不會出現這個問題,因此必須保持位元組數為偶數。
  DataSize = InStrB(DataStart,FormData,DivStr) - DataStart - 1
  FormFieldData = MidB(FormData,DataStart,DataSize)
  Else
  ‘資料長度,減2是因為分隔標誌串前有一個CRLF
  DataSize = InStrB(DataStart,FormData,DivStr) - DataStart - 2
  FormFieldData = bin2str(MidB(FormData,DataStart,DataSize))
  End If
  ‘建立一個Dictionary集儲存Form中各個Field的相關資料
  Set Field = CreateUploadField()
  Field.Name = FormFieldName
  Field.FilePath = FormFileName
  Field.FileName = GetFileName(FormFileName)
  Field.ContentType = FormFileCT
  Field.Length = LenB(FormFieldData)
  Field.Value = FormFieldData
  Fields.Add FormFieldName, Field
  PosOpenBoundary = PosCloseBoundary
  PosCloseBoundary = InStrB(PosOpenBoundary + 1,FormData,DivStr)
  Wend
  Set GetUpload = Fields
  End Function
  ‘把二進位字串轉換成一般字元串函數
  Function bin2str(binstr)
  Dim varlen,clow,ccc,skipflag
  ‘中文字元Skip標誌
  skipflag=0
  ccc = ""
  If Not IsNull(binstr) Then
  varlen=LenB(binstr)
  For i=1 To varlen
  If skipflag=0 Then
  clow = MidB(binstr,i,1)
  ‘判斷是否中文的字元
  If AscB(clow) 〉 127 Then
  ‘AscW會把二進位的中文雙位元組字元高位和低位反轉,所以要先把中文的高低位反轉
  ccc =ccc & Chr(AscW(MidB(binstr,i+1,1) & clow))
  skipflag=1
  Else
  ccc = ccc & Chr(AscB(clow))
  End If
  Else
  skipflag=0
  End If
  Next
  End If
  bin2str = ccc
  End Function
  ‘把一般字元串轉成二進位字串函數
  Function str2bin(varstr)
  str2bin=""
  For i=1 To Len(varstr)
  varchar=mid(varstr,i,1)
  varasc = Asc(varchar)
  ‘ asc對中文字元求出來的值可能為負數,
  ‘ 加上65536就可求出它的無符號數值
  ‘ -1在機器內是用補碼錶示的0xffff,
  ‘ 其無符號值為65535,65535=-1+65536
  ‘ 其他負數依次類推。
  If varasc〈0 Then
  varasc = varasc + 65535
  End If
  ‘對中文的處理:把雙位元組低位和高位分開
  If varasc〉255 Then
  varlow = Left(Hex(Asc(varchar)),2)
  varhigh = right(Hex(Asc(varchar)),2)
  str2bin = str2bin & chrB("&H" & varlow) & chrB("&H" & varhigh)
  Else
  str2bin = str2bin & chrB(AscB(varchar))
  End If
  Next
  End Function
  ‘取得檔案名稱(去掉Path)
  Function GetFileName(FullPath)
  If FullPath 〈〉 "" Then
  FullPath = StrReverse(FullPath)
  FullPath = Left(FullPath, InStr(1, FullPath, "\") - 1)
  GetFileName = StrReverse(FullPath)
  Else
  GetFileName = ""
  End If
  End Function
  〈/SCRIPT〉
  〈SCRIPT RUNAT=SERVER LANGUAGE=JSCRIPT〉
  function CreateUploadField(){ return new uf_Init() }
  function uf_Init(){
  this.Name = null
  this.FileName = null
  this.FilePath = null
  this.ContentType = null
  this.Value = null
  this.Length = null
  }
  〈/SCRIPT〉
  //新聞添加
  〈!--#include file="news_session.asp"--〉
  〈html〉
  〈head〉
  〈meta http-equiv="Content-Language" content="zh-cn"〉
  〈meta http-equiv="Content-Type" content="text/html; charset=gb2312"〉
  〈style type="text/css"〉
  .buttonface {
  BACKGROUND-COLOR: #0079F2; BORDER-BOTTOM: #333333 1px outset; BORDER-LEFT: #333333 1px outset; BORDER-RIGHT: #ffffff 1px outset; BORDER-TOP: #ffffff 1px outset; COLOR: #ffffff; FONT-SIZE: 9pta { color: #000000; text-decoration: none}
  〈/style〉
  〈SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript〉
  〈!--
  function client_onblur(ii) {
  server=eval("form1.server"+ii)
  if(server.value==""){
  client=eval("form1.client"+ii)
  clientvalue=client.value+""
  varlen=clientvalue.length
  a=clientvalue.lastIndexOf(‘\\‘)
  clientvalue=clientvalue.substring(a+1)
  //alert(clientvalue);
  server.value=clientvalue
  }
  }
  function form1_onsubmit() {
  for(i=1;i〈1;i++){
  client=eval("form1.client"+i)
  server=eval("form1.server"+i)
  if(client.value!="" && server.value==""){alert("上傳後的檔案名稱不能空!");server.focus();return false}
  }
  }
  //--〉
  〈/SCRIPT〉
  〈title〉新聞發布系統〈/title〉
  〈/head〉
  〈body bgcolor=#EDF0F5 topmargin=10 marginheight=5 leftmargin=4 marginwidth=0〉
  〈form method="POST" action="news_input.asp" name="form1" enctype="multipart/form-data" LANGUAGE=javascript onsubmit="return form1_onsubmit()"〉
  〈div align="left"〉
  〈table border="1" width="754" height="404"〉
  〈tr align="center"〉
  〈td width="754" height="28" colspan="3" style="font-size:11pt"〉〈strong〉新聞發布系統後台管理--新聞添加〈/strong〉〈/td〉
  〈/tr〉
  〈tr〉
  〈td width="121" height="16" align="center" style="font-size:9pt"〉新聞標題〈/td〉
  〈td width="617" height="16" colspan="2"〉
  〈input type="text" name="news_title" size="87"〉〈/td〉
  〈/tr〉
  〈tr〉
  〈td width="121" height="165" align="center" style="font-size:9pt"〉新聞內容〈/td〉
  〈td width="617" height="165" colspan="2"〉〈textarea rows="11" name="news_content" cols="85"〉〈/textarea〉〈/td〉
  〈/tr〉
  〈tr〉
  〈td width="121" height="21" align="center" style="font-size:9pt"〉新聞來源〈/td〉
  〈td width="617" height="21" colspan="2"〉
  〈input type="text" name="news_src" size="87"〉〈/td〉
  〈/tr〉
  〈tr〉
  〈td width="121" height="20" align="center" style="font-size:9pt" 〉圖片上傳〈/td〉
  〈td width="617" height="20" colspan="2"〉
  〈input type="file" name="client1" size="20" readonly LANGUAGE=javascript onblur="return client_onblur(1)" 〉
  〈span style="font-size:9pt"〉〈/span〉 〈INPUT type="hidden" name="server1"〉 〈input type="hidden" value="mysession" name="mysession"〉 〈/td〉
  〈/tr〉
  〈/table〉
  〈/div〉
  〈p〉
  〈input type="submit" value="遞交" name="B1" class="buttonface"〉 〈input type="reset" value="全部重寫" name="B2" class="buttonface"〉
  〈input type="button" value="帳號修改" onclick="location.href=‘admin/news_chadmin.asp‘" name="B2" style="font-size:10pt;color:#000000;" class="buttonface"〉
  〈input type="button" value="新聞修改" onclick="location.href=‘news_admin1.asp‘" name="B2" style="font-size:10pt;color:#000000;" class="buttonface"〉〈/p〉
  〈/form〉
  〈/body〉
  〈/html〉
  ‘###################
  news_input.asp
  〈!--#include file="upload.inc"--〉
  〈%
  ‘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%〉
  〈script〉
  alert("出錯!新聞標題不可為空");
  history.go(-1);
  //window.location="news_add.asp";
  〈/script〉
  〈%Response.end
  end if
  if len(news_content)=0 then%〉
  〈script〉
  alert("出錯!新聞內容不可為空");
  history.go(-1);
  〈/script〉
  〈%end if
  if len(news_src)=0 then%〉
  〈script〉
  alert("出錯!新聞來源不可為空 ,上海治療陽痿醫院");
  history.go(-1);
  〈/script〉
  〈%Response.end
  end if
  dim varchar
  varchar=right(Fields("server1").value,3)
  if len(varchar)〈〉0 then
  if varchar〈〉"gif" and varchar〈〉"jpg" then
  %〉
  〈script〉
  alert("出錯!不能上傳該圖片類型");
  history.go(-1);
  〈/script〉
  〈% 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+"〈br〉"
  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
  %〉
  〈script〉
  alert("發布成功");
  window.location="news_add.asp";
  〈/script〉
  //新聞列表顯示

asp編程執行個體:用文本+ASP打造新聞發布系統1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.