建议保存为一个独立的ASP文件,以下封装所有函数都可以独立调用与组合使用
<%
'*****************************************************
'获取文件分隔符
function getDivider(binStr)
dim newLine,divider
newLine=chrB(13) & chrB(10) '回车换行符
divider=LeftB(binStr,CLng(InStrB(binStr,newline))-1) 'divider是分割符
getDivider=divider
end function
'*****************************************************
'返回二进制子字符串出现的次数
function getSubStrBCount(binStr,subBinStr)
dim comLen,startIndex,count
comLen=lenB(subBinStr)
startIndex=1
count=0
'遍历二进制数据
do while instrB(startIndex,binStr,subBinStr)<>0 or instrB(startIndex,binStr,subBinStr)<>null
startIndex=instrB(startIndex,binStr,subBinStr)
startIndex=startIndex+comLen
count=count+1
loop
getSubStrBCount=count
end function
'***************************************************
'分割二进制数据,并返回一维数组,如果分隔符中间无数据,默认分割为一次
function SplitB(binStr,splitStr)
dim count,splitStrLen,startIndex,endIndex,binStrLen,i,numCount,num
splitStrLen=LenB(splitStr)
binStrLen=LenB(binStr)
count=getSubStrBCount(binStr,splitStr)
numCount=0
num=0
startIndex=1
endIndex=1
dim binStrList()
redim binStrList(count)
dim result()
if count<>0 then
for i=0 to count
endIndex=InStrB(startIndex,binStr,splitStr)
'开头
if i=0 then
if endIndex=startIndex then
bnumCount=numCount+1
binStrList(i)=""
else
binStrList(i)=MidB(binStr,1,Clng(endIndex-1))
end if
'结尾
elseif i=count then
if endIndex=startIndex then
binStrList(i)=""
else
binStrList(i)=rightB(binstr,Clng(binStrLen-startIndex+1))
end if
exit for
'中部
else
if endIndex=startIndex then
binStrList(i)=""
else
binStrList(i)=MidB(binStr,startIndex,Clng(endIndex-startIndex))
end if
end if
startIndex=Clng(endIndex+splitStrLen)
next
'计算非空元素个数
for i=0 to count
if binStrList(i)<>"" then
numCount=numCount+1
end if
next
'清楚空元素
redim result(numCount-1)
for i=0 to count
if binStrList(i)<>""and num<numCount then
result(num)=binStrList(i)
num=num+1
end if
next
SplitB=result
else
SplitB=binStr
end if
end function
'*****************************************************
'替换特定二进制数据,并返回替换后的结果
function ReplaceB(binStr,comStr,divider)
dim count,splitStrLen,startIndex,endIndex,binStrLen,i,result
splitStrLen=LenB(comStr)
binStrLen=LenB(binStr)
count=getSubStrBCount(binStr,comStr)
startIndex=1
endIndex=1
result=""
dim binStrList()
if count<>0 then
redim binStrList(count)
for i=0 to count
endIndex=InStrB(startIndex,binStr,comStr)
'开头
if i=0 then
if endIndex=startIndex then
bnumCount=numCount+1
binStrList(i)=divider
else
binStrList(i)=MidB(binStr,1,Clng(endIndex-1))+divider
end if
'结尾
elseif i=count then
if endIndex=startIndex then
binStrList(i)=divider
else
binStrList(i)=rightB(binstr,Clng(binStrLen-startIndex+1))
end if
'中部
else
if endIndex=startIndex then
binStrList(i)=divider
else
binStrList(i)=MidB(binStr,startIndex,Clng(endIndex-startIndex))+divider
end if
end if
startIndex=Clng(endIndex+splitStrLen)
result=result+binStrList(i)
next
else
result=binStr
end if
ReplaceB=result
end function
'*****************************************************
'转换字符串为二进制,单引号双引号除外
function BinAsc(str)
dim i,result
result=""
if str<>"" or str<>null then
for i=1 to len(str)
result=result+ChrB(Asc(mid(str,i,1)))
next
BinAsc=result
else
BinAsc=false
end if
end function
'*****************************************************
'获取单个文件名
function getFileName(binStrPath)
dim list
list=SplitB(binStrPath,binAsc("/"))
if isArray(list) then
getFileName=list(ubound(list))
else
getFileName=list
end if
end function
'*****************************************************
'获取文件上传本地路径列表,如果结果只有一个文件返回单个字节型数据
function getFilePath(binStr)
dim dataList,i,spiliter,divider,fileNameStrB,quotedB,firstIndex,lastIndex
firstIndex=1
lastIndex=1
divider=getDivider(binStr)
dataList=SplitB(binStr,divider)
fileNameStrB=BinAsc("filename=")
quotedB=chrB(34)
dim result()
if isArray(dataList) then
redim result(Cint(ubound(dataList)-1))
for i=0 to Cint(ubound(dataList)-1)
firstIndex=InStrB(dataList(i),fileNameStrB+quotedB)+lenB(fileNameStrB+quotedB)
lastIndex=InStrB(firstIndex,dataList(i),quotedB)
result(i)=midB(dataList(i),firstIndex,Clng(lastIndex-firstIndex))
next
getFilePath=result
else
firstIndex=InStrB(dataList,fileNameStrB+quotedB)+lenB(fileNameStrB+quotedB)
lastIndex=InStrB(firstIndex,dataList,quotedB)
getFilePath=midB(dataList,firstIndex,Clng(lastIndex-firstIndex))
end if
end function
'*****************************************************
'获取文件类型列表,如果结果只有一个文件返回单个字节型数据
function getFileType(binStr)
dim dataList,i,spiliter,divider,fileNameStrB,newLine,firstIndex,lastIndex
firstIndex=1
lastIndex=1
divider=getDivider(binStr)
dataList=SplitB(binStr,divider)
fileNameStrB=BinAsc("Content-Type:")
newLine=chrB(13) & chrB(10) '回车换行符
dim result()
if isArray(dataList) then
redim result(Cint(ubound(dataList)-1))
for i=0 to Cint(ubound(dataList)-1)
firstIndex=InStrB(dataList(i),fileNameStrB)+lenB(fileNameStrB)
lastIndex=InStrB(firstIndex,dataList(i),newLine)
result(i)=midB(dataList(i),firstIndex,Clng(lastIndex-firstIndex))
next
getFileType=result
else
firstIndex=InStrB(dataList,fileNameStrB)+lenB(fileNameStrB)
lastIndex=InStrB(firstIndex,dataList,newLine)
getFileType=midB(dataList,firstIndex,Clng(lastIndex-firstIndex))
end if
end function
'*****************************************************
'获取文件内容列表,如果结果只有一个文件返回单个字节型数据
function getFileData(binStr)
dim dataList,i,spiliter,divider,doubleLineB,newLine,firstIndex,lastIndex
firstIndex=1
lastIndex=1
divider=getDivider(binStr)
dataList=SplitB(binStr,divider)
doubleLineB=chrB(13) & chrB(10)&chrB(13) & chrB(10)
newLine=chrB(13) & chrB(10) '回车换行符
dim result()
if isArray(dataList) then
redim result(Cint(ubound(dataList)-1))
for i=0 to Cint(ubound(dataList)-1)
firstIndex=InStrB(dataList(i),doubleLineB)+lenB(doubleLineB)
cutLen=Clng(lenB(dataList(i))-lenB(newLine)-firstIndex)
result(i)=midB(dataList(i),firstIndex,cutLen)
next
getFileData=result
else
firstIndex=InStrB(dataList,doubleLineB)+lenB(doubleLineB)
cutLen=Clng(lenB(dataList)-lenB(newLine)-firstIndex)
getFileData=midB(dataList,firstIndex,cutLen)
end if
end function
'*****************************************************
'获取上传文件控件名列表,如果结果只有一个文件返回单个字节型数据
function getUploadFileName(binStr)
dim dataList,i,spiliter,divider,fileNameStrB,quotedB,firstIndex,lastIndex
firstIndex=1
lastIndex=1
divider=getDivider(binStr)
dataList=SplitB(binStr,divider)
fileNameStrB=BinAsc("name=")
quotedB=chrB(34)
dim result()
if isArray(dataList) then
redim result(Cint(ubound(dataList)-1))
for i=0 to Cint(ubound(dataList)-1)
firstIndex=InStrB(dataList(i),fileNameStrB+quotedB)+lenB(fileNameStrB+quotedB)
lastIndex=InStrB(firstIndex,dataList(i),quotedB)
result(i)=midB(dataList(i),firstIndex,Clng(lastIndex-firstIndex))
next
getUploadFileName=result
else
firstIndex=InStrB(dataList,fileNameStrB+quotedB)+lenB(fileNameStrB+quotedB)
lastIndex=InStrB(firstIndex,dataList,quotedB)
getUploadFileName=midB(dataList,firstIndex,Clng(lastIndex-firstIndex))
end if
end function
%>