欢迎光临
我们一直在努力

中文的无组件文件上传ASP函数

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

说明:持中文的无组件文件上传asp函数,由于asp不支持二进制写入文件,所以存成文件时必须使用组件,本函数只提供截取上传文件的数据,可以写入到数据库。

<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
}

示例

【woozhj】 于 00-4-17 上午 09:37:43 加贴在 joy asp ↑:

文件:uploadtest.asp
<html>
<head>
<title>untitled document</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>

<body bgcolor="#ffffff">
<form  method="post" name="form1" enctype="multipart/form-data" action="showdata.asp">
  <p>text1:
    <input type="text" name="text1">
  </p>
  <p>text2:
    <input type="text" name="text2">
  </p>
  <p>txtarea:
    <textarea name="textfield" cols="20" rows="10"></textarea>
  </p>
  <p>file:
    <input type="file" name="newfile">
  </p>
  <p>
    <input type="submit" name="submit" value="submit">
    <input type="reset" name="reset" value="reset">
  </p>
</form>
</body>
</html>

文件:showdata.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
      formsize=request.totalbytes
      formdata=request.binaryread(formsize)
      set fields = getupload(formdata)
      response.write "text1:" & fields("text1").value & "<br>" & vbcrlf
      response.write "text2:" & fields("text2").value & "<br>" & vbcrlf
      response.write "textarea:" & fields("textfield").value & "<br>" & vbcrlf
      response.write fields("newfile").filename
      response.write fields("newfile").contenttype
      response.contenttype = fields("newfile").contenttype
      if fields("newfile").filename<>"" then
         response.contenttype = fields("newfile").contenttype
         response.binarywrite fields("newfile").value
      end if

      response.binarywrite formdata
    %>

wakeful】 于 00-4-15 下午 08:30:49 加贴在 joy asp ↑:
file:upload.asp
<%
author philippe collignon
email phcollignon@email.com

sub builduploadrequest(requestbin)
    get the boundary
    posbeg = 1
    posend = instrb(posbeg,requestbin,getbytestring(chr(13)))
    boundary = midb(requestbin,posbeg,posend-posbeg)
    boundarypos = instrb(1,requestbin,boundary)
    get all data inside the boundaries
    do until (boundarypos=instrb(requestbin,boundary & getbytestring("–")))
        members variable of objects are put in a dictionary object
        dim uploadcontrol
        set uploadcontrol = createobject("scripting.dictionary")
        get an object name
        pos = instrb(boundarypos,requestbin,getbytestring("content-disposition"))
        pos = instrb(pos,requestbin,getbytestring("name="))
        posbeg = pos+6
        posend = instrb(posbeg,requestbin,getbytestring(chr(34)))
        name = getstring(midb(requestbin,posbeg,posend-posbeg))
        posfile = instrb(boundarypos,requestbin,getbytestring("filename="))
        posbound = instrb(posend,requestbin,boundary)
        test if object is of file type
        if  posfile<>0 and (posfile<posbound) then
            get filename, content-type and content of file
            posbeg = posfile + 10
            posend =  instrb(posbeg,requestbin,getbytestring(chr(34)))
            filename = getstring(midb(requestbin,posbeg,posend-posbeg))
            add filename to dictionary object
            uploadcontrol.add "filename", filename
            pos = instrb(posend,requestbin,getbytestring("content-type:"))
            posbeg = pos+14
            posend = instrb(posbeg,requestbin,getbytestring(chr(13)))
            add content-type to dictionary object
            contenttype = getstring(midb(requestbin,posbeg,posend-posbeg))
            uploadcontrol.add "contenttype",contenttype
            get content of object
            posbeg = posend+4
            posend = instrb(posbeg,requestbin,boundary)-2
            value = midb(requestbin,posbeg,posend-posbeg)
            else
            get content of object
            pos = instrb(pos,requestbin,getbytestring(chr(13)))
            posbeg = pos+4
            posend = instrb(posbeg,requestbin,boundary)-2
            value = getstring(midb(requestbin,posbeg,posend-posbeg))
        end if
        add content to dictionary object
    uploadcontrol.add "value" , value
        add dictionary object to main dictionary
    uploadrequest.add name, uploadcontrol
        loop to next object
        boundarypos=instrb(boundarypos+lenb(boundary),requestbin,boundary)
    loop

end sub

string to byte string conversion
function getbytestring(stringstr)
for i = 1 to len(stringstr)
     char = mid(stringstr,i,1)
    getbytestring = getbytestring & chrb(ascb(char))
next
end function

byte string to string conversion
function getstring(stringbin)
getstring =""
for intcount = 1 to lenb(stringbin)
    getstring = getstring & chr(ascb(midb(stringbin,intcount,1)))
next
end function
%>

outputfile.asp

<%
author philippe collignon
email phcollignon@email.com

response.expires=0
response.buffer = true
response.clear
response.binarywrite(request.binaryread(request.totalbytes))
bytecount = request.totalbytes
response.binarywrite(request.binaryread(varbytecount))

requestbin = request.binaryread(bytecount)
dim uploadrequest
set uploadrequest = createobject("scripting.dictionary")

builduploadrequest  requestbin

email = uploadrequest.item("email").item("value")

contenttype = uploadrequest.item("blob").item("contenttype")
filepathname = uploadrequest.item("blob").item("filename")
filename = right(filepathname,len(filepathname)-instrrev(filepathname,"\"))
value = uploadrequest.item("blob").item("value")

create filesytemobject component
set scriptobject = server.createobject("scripting.filesystemobject")

create and write to a file
pathend = len(server.mappath(request.servervariables("path_info")))-14
set myfile = scriptobject.createtextfile(left(server.mappath(request.servervariables("path_info")),pathend)&"uploaded"&filename)

for i = 1 to lenb(value)
     myfile.write chr(ascb(midb(value,i,1)))
next

myfile.close
%>
<b>uploaded file : </b><%="uploaded"&filename%><br>
<img src="<%="uploaded"&filename%>">
<!–#include file="upload.asp"–>

outputclient.asp

<%
author philippe collignon
email phcollignon@email.com

response.buffer = true
response.clear

bytecount = request.totalbytes

requestbin = request.binaryread(bytecount)
dim uploadrequest
set uploadrequest = createobject("scripting.dictionary")

builduploadrequest  requestbin

email = uploadrequest.item("email").item("value")

contenttype = uploadrequest.item("blob").item("contenttype")
filepathname = uploadrequest.item("blob").item("filename")
filename = right(filepathname,len(filepathname)-instrrev(filepathname,"\"))
value = uploadrequest.item("blob").item("value")
%>

your email is : <%= email %><br>
file name of you picture is <%=filepathname%><br>
file type of your picture is <%=contenttype%><br>

<!–#include file="upload.asp"–>
binaryoutputclient.asp

<%
author philippe collignon
email phcollignon@email.com

response.buffer = true
response.clear
bytecount = request.totalbytes

requestbin = request.binaryread(bytecount)
dim uploadrequest
set uploadrequest = createobject("scripting.dictionary")

builduploadrequest  requestbin

email = uploadrequest.item("email").item("value")

contenttype =  uploadrequest.item("blob").item("contenttype")
filepathname = uploadrequest.item("blob").item("filename")
filename = right(filepathname,len(filepathname)-instrrev(filepathname,"\"))
picture = uploadrequest.item("blob").item("value")

response.contenttype = contenttype
response.binarywrite picture

%>
<!–#include file="upload.asp"–>
uploadform.htm

<!– author philippe collignon –>
<!– email phcollignon@email.com –>
<html>
<head>
    <title>upload form</title>
</head>
<body>
<b>output to client</b>
<form method="post" enctype="multipart/form-data" action="outputclient.asp">
email : <input type="text" name="email" value="phcollignon@email.com"><br>
picture : <input type="file" name="blob"><br>
<input type="submit" name="enter">
</form>
<b>binary output to client</b>
<form method="post" enctype="multipart/form-data" action="binaryoutputclient.asp">
email : <input type="text" name="email" value="phcollignon@email.com"><br>
picture : <input type="file" name="blob"><br>
<input type="submit" name="enter">
</form>
<b>output to file system</b>
<form method="post" enctype="multipart/form-data" action="outputfile.asp">
email : <input type="text" name="email" value="phcollignon@email.com"><br>
picture : <input type="file" name="blob"><br>
<input type="submit" name="enter">
</form>
</body>
</html>

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