Asp下实现多表单域无组件文件上传的实例

2018-09-06 11:00

阅读:496

  最近经常有人问到这类问题,在此转贴一下,内容:
1。数据库表结构(Access):
UserID:Text(保存上传文件的用户ID)
FileContentType:Text(用来保存上传文件的类型,eg:Application/msword,主要用来使用户能正确下载此文件)
FileContent:OLEObject(保存文件数据)

2。HTML文件
muploadfile.htm
<Formname=upload_fileenctype=multipart/form-dataaction=muploadfile.aspmethod=post>
<inputtype=hiddenname=UserIDvalue=abc>
<inputtype=hiddenname=FileUploadStart>这里用来表示开始文件数据上传
Filetosend:

<INPUTTYPE=filename=file_upsize=30><br>
<INPUTTYPE=filename=file_upsize=30><br>
<inputtype=hiddenname=FileUploadEnd>这里用来表示文件数据结束
<inputtype=submitvalue=Submit>
</Form></P><P>
3。ASP文件
muploadfile.asp</P><P><%
Response.Expires=0
Functionbin2str(binstr)
Dimvarlen,clow,ccc,skipflag</P><P>skipflag=0
ccc=
IfNotIsNull(binstr)Then
varlen=LenB(binstr)
Fori=1Tovarlen
Ifskipflag=0Then
clow=MidB(binstr,i,1)
IfAscB(clow)>127Then
ccc=ccc&Chr(AscW(MidB(binstr,i+1,1)&clow))
skipflag=1
Else
ccc=ccc&Chr(AscB(clow))
EndIf
Else
skipflag=0
EndIf
Next
EndIf
bin2str=ccc
EndFunction</P><P>
varByteCount=Request.TotalBytes
bnCRLF=chrB(13)&chrB(10)
binHTTPHeader=Request.BinaryRead(varByteCount)
Divider=LEFTB(binHTTPHeader,INSTRB(binHTTPHeader,bnCRLF)-1)</P><P>开始读非文件域的数据
DowhilelenB(binHTTPHeader)>46

binHeaderData=LeftB(binHTTPHeader,INSTRB(binHTTPHeader,bnCRLF&bnCRLF)-1)
strHeaderData=bin2str(binHeaderData)</P><P>lngFieldNameStart=Instr(strHeaderData,name=&chr(34))+Len(name=&chr(34))
lngFieldNameEnd=Instr(lngFieldNameStart,strHeaderData,chr(34))


strFieldName=Mid(strHeaderData,lngFieldNameStart,lngFieldNameEnd-lngFieldNameStart)
strFieldName=Trim(strFieldName)
strFieldName=Replace(strFieldName,vbcrlf,vbnullstring)

判断文件数据时候开始
IfstrComp(strFieldName,FileUploadStart,1)=0Then
binHTTPHeader=MIDB(binHTTPHeader,INSTRB(DataStart+1,binHTTPHeader,divider))
exitdo
Endif

DataStart=INSTRB(binHTTPHeader,bnCRLF&bnCRLF)+4
DataEnd=INSTRB(DataStart+1,binHTTPHeader,divider)-DataStart</P><P>binFieldValue=MIDB(binHTTPHeader,DataStart,DataEnd)
strFieldValue=bin2str(binFieldValue)
strFieldValue=Trim(strFieldValue)
strFieldValue=Replace(strFieldValue,vbcrlf,vbnullstring)</P><P>非文件上传域变量赋值
executestrFieldName&=&strFieldValue&


binHTTPHeader=MIDB(binHTTPHeader,INSTRB(DataStart+1,binHTTPHeader,divider))

loop</P><P>开始处理文件数据
DowhilelenB(binHTTPHeader)>46


binHeaderData=LeftB(binHTTPHeader,INSTRB(binHTTPHeader,bnCRLF&bnCRLF)-1)

strHeaderData=bin2str(binHeaderData)

读取上传文件的Content-Type
lngFileContentTypeStart=Instr(strHeaderData,Content-Type:)+Len(Content-Type:)
strFileContentType=Trim(Mid(strHeaderData,lngFileContentTypeStart))
strFileContentType=Replace(strFileContentType,vbCRLF,vbNullString)

读取上传的文件名
lngFileNameStart=Instr(strHeaderData,filename=&chr(34))+Len(filename=&chr(34))
lngFileNameEnd=Instr(lngFileNameStart,strHeaderData,chr(34))
strFileName=Mid(strHeaderData,lngFileNameStart,lngFileNameEnd-lngFileNameStart)
strFileName=Trim(strFileName)
strFileName=Replace(strFileName,vbCRLF,vbNullString)

读取上传文件数据
DataStart=INSTRB(binHTTPHeader,bnCRLF&bnCRLF)+4
DataEnd=INSTRB(DataStart+1,binHTTPHeader,divider)-DataStart

IfstrFileName<>Then

binFieldValue=MIDB(binHTTPHeader,DataStart,DataEnd)

将上传的文件写入数据库
conn.OpenDSN=abc

SQL=select*fromUser_File
setrs=server.CreateObject(ADODB.Recordset)
rs.Opensql,conn,3,3
rs.addnew
rs(UserID)=UserID
rs(FileContentType)=strFileContentType
rs(FileContent).AppendChunkbinFieldValue
rs.update
rs.close
setrs=Nothing
conn.Close
setconn=Nothing

Endif

binHTTPHeader=MIDB(binHTTPHeader,INSTRB(DataStart+1,binHTTPHeader,divider))

loop
%>


4。下载用户上传的文件
<%
Response.Buffer=true
Response.Clear

UserID=request(UserID)</P><P>Setconn=server.createobject(adodb.connection)
setrs=server.createobject(adodb.recordset)
conn.openDSN=UploadFile
rs.openselect*fromUser_FilewhereUserID=&UserID&,conn,3,3
Response.ContentType=rs(FileContentType)</P><P>lngOffset=0
conChunkSize=1024
lngPictSize=rs(FileContent).ActualSize
DoWhilelngOffset<lngPictSize
varChunk=rs(FileContent).GetChunk(conChunkSize)
Response.BinaryWritevarChunk
lngOffset=lngOffset+conChunkSize
IflngOffset>lngPictSizeThenExitDo
Loop


rs.close
setrs=Nothing
conn.close
setconn=nothing
%></P><P>就是这些了,希望此方法对大家能有所帮助。:)


评论


亲,登录后才可以留言!