关键词:asp
作者:woozhj
文件名:upload.inc
说明:支持中文的无组件文件上传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
}
</script>
(转自chinaasp)
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器