化境ASP无组件上传

作者:网络 来源:佚名 更新时间:2008-07-07 10:57:20 点击:

  一直以来,由于filesystemobject的局限,所以asp最大的难题就是文件上传,大多解决法就是安装第三方上传组件。可第三方组件有很多问题,有的组件要注册,有的组件要在表单中加上他的版权信息。还有的就是组件的兼容问题。在网上也流传了很多无组件上传的代码,但都是只能上传文本文件,或是只能将文件上传到数据库中。作者这段时间在研究asp,发现可以不用第三方组件上传任意类型的文件。就写了这个类,给大家一个方便,整个类放在一个文件:upload_5xsoft.inc 在 example 目录下还有一个完整的多文件上传示例程序,可以直接使用。

  由于《化境无组件上传程序》问题很多,很多网友都发来他们自己的修改版,各有特点。这里,作者采用了网友梁无惧给出的修改版,再作了些修改,解决了原来的已发现的所有问题(具体看说明)。上传速度也有了很大的提高。

  upload_5xsoft.inc

  <script runat=server language=vbscript>

'''''''''''''''''''''''''''''''''''''''''''''''''
'
'请保留此信息: 稻香老农制作 
'
'''''''''''''''''''''''''''''''''''''''''''''''''

dim upfile_5xsoft_stream

class upload_5xsoft
 
dim form,file,version
 
private sub class_initialize
dim istart,ifilenamestart,ifilenameend,iend,vbenter,iformstart,iformend,thefile
dim strdiv,mformname,mformvalue,mfilename,mfilesize,mfilepath,idivlen,mstr
version="化境编程界http上传程序 version 1.0"
if request.totalbytes<1 then exit sub
set form=createobject("scripting.dictionary")
set file=createobject("scripting.dictionary")
set upfile_5xsoft_stream=createobject("adodb.stream")
upfile_5xsoft_stream.mode=3
upfile_5xsoft_stream.type=1
upfile_5xsoft_stream.open
upfile_5xsoft_stream.write request.binaryread(request.totalbytes)

vbenter=chr(13)&chr(10)
idivlen=instring(1,vbenter)+1
strdiv=substring(1,idivlen)
iformstart=idivlen
iformend=instring(iformstart,strdiv)-1
while iformstart < iformend
  istart=instring(iformstart,"name=""")
  iend=instring(istart+6,"""")
  mformname=substring(istart+6,iend-istart-6)
  ifilenamestart=instring(iend+1,"filename=""")
  if ifilenamestart>0 and ifilenamestart<iformend then
   ifilenameend=instring(ifilenamestart+10,"""")
   mfilename=substring(ifilenamestart+10,ifilenameend-ifilenamestart-10)
   istart=instring(ifilenameend+1,vbenter&vbenter)
   iend=instring(istart+4,vbenter&strdiv)
   if iend>istart then
    mfilesize=iend-istart-4
   else
    mfilesize=0
   end if
   set thefile=new fileinfo
   thefile.filename=getfilename(mfilename)
   thefile.filepath=getfilepath(mfilename)
   thefile.filesize=mfilesize
   thefile.filestart=istart+4
   thefile.formname=formname
   file.add mformname,thefile
  else
   istart=instring(iend+1,vbenter&vbenter)
   iend=instring(istart+4,vbenter&strdiv)

   if iend>istart then
    mformvalue=substring(istart+4,iend-istart-4)
   else
    mformvalue=""
   end if
   form.add mformname,mformvalue
  end if

  iformstart=iformend+idivlen
  iformend=instring(iformstart,strdiv)-1
wend
end sub

private function substring(thestart,thelen)
 dim i,c,stemp
 upfile_5xsoft_stream.position=thestart-1
 stemp=""
 for i=1 to thelen
   if upfile_5xsoft_stream.eos then exit for
   c=ascb(upfile_5xsoft_stream.read(1))
   if c > 127 then
    if upfile_5xsoft_stream.eos then exit for
    stemp=stemp&chr(ascw(chrb(ascb(upfile_5xsoft_stream.read(1)))&chrb(c)))
    i=i+1
   else
    stemp=stemp&chr(c)
   end if
 next
 substring=stemp
end function

private function instring(thestart,varstr)
 dim i,j,bt,thelen,str
 instring=0
 str=tobyte(varstr)
 thelen=lenb(str)
 for i=thestart to upfile_5xsoft_stream.size-thelen
   if i>upfile_5xsoft_stream.size then exit function
   upfile_5xsoft_stream.position=i-1
   if ascb(upfile_5xsoft_stream.read(1))=ascb(midb(str,1)) then
    instring=i
    for j=2 to thelen
      if upfile_5xsoft_stream.eos then
        instring=0
        exit for
      end if
      if ascb(upfile_5xsoft_stream.read(1))<>ascb(midb(str,j,1)) then
        instring=0
        exit for
      end if
    next
    if instring<>0 then exit function
   end if
 next
end function

private sub class_terminate 
  form.removeall
  file.removeall
  set form=nothing
  set file=nothing
  upfile_5xsoft_stream.close
  set upfile_5xsoft_stream=nothing
end sub
  
 
 private function getfilepath(fullpath)
  if fullpath <> "" then
   getfilepath = left(fullpath,instrrev(fullpath, "\"))
  else
   getfilepath = ""
  end if
 end  function
 
 private function getfilename(fullpath)
  if fullpath <> "" then
   getfilename = mid(fullpath,instrrev(fullpath, "\")+1)
  else
   getfilename = ""
  end if
 end  function

 private function tobyte(str)
   dim i,icode,c,ilow,ihigh
   tobyte=""
   for i=1 to len(str)
   c=mid(str,i,1)
   icode =asc(c)
   if icode<0 then icode = icode + 65535
   if icode>255 then
     ilow = left(hex(asc(c)),2)
     ihigh =right(hex(asc(c)),2)
     tobyte = tobyte & chrb("&h"&ilow) & chrb("&h"&ihigh)
   else
     tobyte = tobyte & chrb(ascb(c))
   end if
   next
 end function
end class


class fileinfo
  dim formname,filename,filepath,filesize,filestart
  private sub class_initialize
    filename = ""
    filepath = ""
    filesize = 0
    filestart= 0
    formname = ""
  end sub
 
 public function saveas(fullpath)
    dim dr,errorchar,i
    saveas=1
    if trim(fullpath)="" or filesize=0 or filestart=0 or filename="" then exit function
    if filestart=0 or right(fullpath,1)="/" then exit function
    set dr=createobject("adodb.stream")
    dr.mode=3
    dr.type=1
    dr.open
    upfile_5xsoft_stream.position=filestart-1
    upfile_5xsoft_stream.copyto dr,filesize
    dr.savetofile fullpath,2
    dr.close
    set dr=nothing
    saveas=0
  end function
end class
</script>

  upfile.asp

<%option explicit%>
<!--#include file="upload_5xsoft.inc"-->
<html>
<head>
<title>文件上传</title>
</head>
<body>
<br>化境文件上传!<hr size=1 noshadow width=300 align=left><br><br>

</body>
</html>

  upfile.htm

<html>
<head>
<title>untitled document</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
td {  font-size: 9pt}
a {  color: #000000; text-decoration: none}
a:hover {  text-decoration: underline}
.tx {  height: 16px; width: 30px; border-color: black black #000000; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; font-size: 9pt; background-color: #eeeeee; color: #0000ff}
.bt {  font-size: 9pt; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; height: 16px; width: 80px; background-color: #eeeeee; cursor: hand}
.tx1 { height: 20px; width: 30px; font-size: 9pt; border: 1px solid; border-color: black black #000000; color: #0000ff}
-->
</style>
</head>

<body bgcolor="#ffffff" text="#000000">
<form name="form1" method="post" action="upfile.asp" enctype="multipart/form-data" >
  <table border="1" cellspacing="0" cellpadding="0" bordercolorlight="#000000" bordercolordark="#cccccc" width="91" height="23">
    <tr>
      <td align="left" valign="middle" height="18" width="18">&nbsp;</td>
      <td bgcolor="#cccccc" align="left" valign="middle" height="18" width="67"> 文件上传</td>
    </tr>
  </table>
  <br>
  <input type="hidden" name="act" value="upload">
  <br>
  <table width="71%" border="1" cellspacing="0" cellpadding="5" align="center" bordercolordark="#cccccc" bordercolorlight="#000000">
    <tr bgcolor="#cccccc">
      <td height="22" align="left" valign="middle" bgcolor="#cccccc">&nbsp;化境编程界文件上传</td>
    </tr>
    <tr align="left" valign="middle" bgcolor="#eeeeee">
      <td bgcolor="#eeeeee" height="92">
        <script language="javascript">
   function setid()
   {
   str='<br>';
   if(!window.form1.upcount.value)
    window.form1.upcount.value=1;
    for(i=1;i<=window.form1.upcount.value;i++)
      str+='文件'+i+':<input type="file" name="file'+i+'" class="tx1"><br><br>';
   window.upid.innerhtml=str+'<br>';
   }
   </script>
        <li> 需要上传的个数
          <input type="text" name="upcount" class="tx" value="1">
          <input type="button" name="button" class="bt" value="· 设定 ·">
        </li>
        <br>
        <br>
        <li>上传到:
          <input type="text" name="filepath" class="tx" value="">
        </li>
      </td>
    </tr>
    <tr align="center" valign="middle">
      <td align="left" id="upid" height="122"> 文件1:
        <input type="file" name="file1" class="tx1" value="">
      </td>
    </tr>
    <tr align="center" valign="middle" bgcolor="#eeeeee">
      <td bgcolor="#eeeeee" height="24">
        <input type="submit" name="submit" value="· 提交 ·" class="bt">
        <input type="reset" name="submit2" value="· 重执 ·" class="bt">
      </td>
    </tr>
  </table>
</form>
</body>
</html>
<script language="javascript">

setid();
</script>