以下源代码为一个文件
<%
作者:playxp,email:playxp@sohu.com有问题请与我联系
功能:纯asp分离form数据中的二进制文件和文本数据(支持中文)。
使用方法:将此文件包含在处理form的asp文件中,使用getvalue(name)来读取表单中各个元素的值。
注意:一定不要使用request.form来读取数据。
在绝大数的支持asp的主页空间仅能使用有限的组件,不能使用上传组件,而内置的request对象又不
支持读取二进制文件,因此使用该文件可以解决上传文件困难的弊病,但在性能上却大打折扣。本程序参考了网上的一些代码。
由gbk向unicode转换
function gbtou(binstr,bytenum)
gbtou=""
for j=1 to bytenum
gbcode1=ascb(midb(binstr,j,1))
if gbcode1>&h80 then
gbcode2=ascb(midb(binstr,j+1,1))
a=gbcode1-&h81
b=gbcode2-&h40
if gbcode2>&h7f then b=b-1
gbtou=gbtou & chrw(gb2u(a*190+b)) gb2u为转换表
j=j+1
else
gbtou=gbtou & chrw(ascb(midb(binstr,j,1)))
end if
next
end function
得到边界字符串
boundary=mid(request.servervariables("content_type"),31)
form中数据的字节数
bytecount=request.totalbytes
form中的二进制数据
binread=request.binaryread(bytecount)
边界ascii字符串
endstr=chrb(13)&chrb(10)&chrb(45)&chrb(45)
for i=1 to len(boundary)
endstr=endstr&chrb(ascb(mid(boundary,i,1)))
next
dim formvalue(31,3)
dim bytei 字节指针
e=0
bytei=37+len(boundary) 第一个元素数据开始处
do while bytei<bytecount
if ascb(midb(binread,bytei,1))=110 then 元素数据存在
formvalue(e,0)=""
formvalue(e,1)=""
formvalue(e,2)=""
formvalue(e,3)=""
bytei=bytei+6 名字开始处
namestart=bytei
bytei=instrb(bytei,binread,chrb(34)) 名字结束处
namelen=bytei-namestart 名字长度
formvalue(e,0)=gbtou(midb(binread,namestart,namelen),namelen) 读入名字
if ascb(midb(binread,bytei+1,1))=13 then 元素为文本
bytei=bytei+5 文本数据开始处
datastart=bytei
bytei=instrb(bytei,binread,endstr) 文本数据结束处
datalen=bytei-datastart 文本数据长度
formvalue(e,1)=gbtou(midb(binread,datastart,datalen),datalen) 读入文本数据
else 元素为文件
bytei=bytei+13 路径开始处
pathstart=bytei
bytei=instrb(bytei,binread,chrb(34)) 路径结束处
pathlen=bytei-pathstart
formvalue(e,2)=gbtou(midb(binread,pathstart,pathlen),pathlen) 读入路径
bytei=bytei+17 文件类型开始
typestart=bytei
bytei=instrb(bytei,binread,chrb(13)) 文件类型结束处
typelen=bytei-typestart 文件类型长度
formvalue(e,3)=gbtou(midb(binread,typestart,typelen),typelen) 读入文件类型
bytei=bytei+4 文件数据开始处
datastart=bytei
bytei=instrb(bytei,binread,endstr) 文件数据结束处
datalen=bytei-datastart 文件数据长度
formvalue(e,1)=midb(binread,datastart,datalen) 读入文件数据
end if
bytei=bytei+38+len(boundary) 移向下一元素数据开始处
e=e+1
else
exit do
end if
loop
根据元素名来检索元素数据
如果表单元素为文本数据直接返回其值
如果表单元素为二进制数据文件,则返回一3元素数组,第一个元素为文件的二进制数据,
第二个元素为文件的路径,第三个为文件的类型。
function getvalue(name)
for i=0 to 31
if strcomp(formvalue(i,0),name,1)=0 then
if formvalue(i,3)="" then
getvalue=formvalue(i,1)
else
getvalue=array(formvalue(i,1),formvalue(i,2),formvalue(i,3))
end if
exit function
end if
next
getvalue=null
end function
%>
