欢迎光临
我们一直在努力

多个表单和多个图片一起上传完美解决方案

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

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>
<!—————————————————->
在提交的叶面使用
<form method="post" enctype="multipart/form-data">
保存的叶面
将upload.inc包含
<!–#include file="upload.inc"–>
<%

function lngconvert2(strtemp)       
str1=leftb(strtemp,1)       
str2=rightb(strtemp,1)       
lngconvert2 = clng(ascb(str2) + ((ascb(str1) * 256)))       
end function       
       
function lngconvert(strtemp)       
str1=leftb(strtemp,1)       
str2=rightb(strtemp,1)       
len1=ascb(str1)       
len2=ascb(str2)       
lngconvert = clng(ascb(str1) + ascb(str2) * 256)       
end function
      
dim formdata,formsize       
formsize=request.totalbytes       
formdata=request.binaryread(formsize)       
      
set fields = getupload(formdata)       
if fields("newfile").filename<>"" then       
tempstr=leftb(fields("newfile").value,10)       
tstr=chrb(255)&chrb(216)&chrb(255)&chrb(224)&chrb(0)&chrb(16)&chrb(74)&chrb(70)&chrb(73)&chrb(70)       
end if       
       
提交页面的表单内容
txt1=fields("txt1").value
txt2=fields("txt2").value
txt3=fields("txt3").value
     
set rs = server.createobject("adodb.recordset")       
sql="select * from news"       
rs.open sql,conn,1,3     
插入纪录  
rs.addnew       
rs("title")=title
rs("body")=content
rs("pub")=from
rs("up_date")=now()
set field=rs.fields("pic")       
field.appendchunk fields("newfile").value       
多个图片一样处理
rs.update       
rs.close       
conn.close       
set rs=nothing       
set conn=nothing       
%>

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

相关推荐

  • 暂无文章