欢迎光临
我们一直在努力

不使用组件实现多个图片与文本数据同时写入数据库(1)

建站超值云服务器,限时71元/月

filename: upload.inc
—————————————————————————
——————————————
<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>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 不使用组件实现多个图片与文本数据同时写入数据库(1)
分享到: 更多 (0)