Asp下实现多表单域无组件文件上传的实例
网络编程 2021-07-05 10:58www.168986.cn编程入门
最近经常有人问到这类问题,在此转贴一下,内容:
1。数据库表结构(Aess)
UserID:Text(保存上传文件的用户ID)
FileContentType:Text(用来保存上传文件的类型,eg:"Application/msword",主要用来使用户能正确下载此文件)
FileContent:OLE Object(保存文件数据)
2。HTML文件
muploadfile.htm
<Form name="upload_file" enctype="multipart/form-data" action="muploadfile.asp" method=post>
<input type=hidden name="UserID" value="abc">
<input type=hidden name="FileUploadStart"> '这里用来表示开始文件数据上传
File to send:
<INPUT TYPE="file" name="file_up" size="30"><br>
<INPUT TYPE="file" name="file_up" size="30"><br>
<input type=hidden name="FileUploadEnd"> '这里用来表示文件数据结束
<input type=submit value=Submit>
</Form></P><P>
3。ASP文件
muploadfile.asp</P><P><%
Response.Expires=0
Function bin2str(binstr)
Dim varlen,clow,c,skipflag </P><P> skipflag=0
c = ""
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
c =c & Chr(AscW(MidB(binstr,i+1,1) & clow))
skipflag=1
Else
c = c & Chr(AscB(clow))
End If
Else
skipflag=0
End If
Next
End If
bin2str = c
End Function </P><P>
varByteCount = Request.TotalBytes
bnCRLF = chrB( 13 ) & chrB( 10 )
binHTTPHeader=Request.BinaryRead(varByteCount)
Divider = LEFTB( binHTTPHeader, INSTRB( binHTTPHeader, bnCRLF ) - 1 )</P><P>'开始读非文件域的数据
Do while lenB(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)
'判断文件数据时候开始
If strComp(strFieldName,"FileUploadStart",1)=0 Then
binHTTPHeader=MIDB(binHTTPHeader,INSTRB( DataStart + 1, binHTTPHeader, divider ))
exit do
End if
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> '非文件上传域变量赋值
execute strFieldName&"="""&strFieldValue&""""
binHTTPHeader=MIDB(binHTTPHeader,INSTRB( DataStart + 1, binHTTPHeader, divider ))
loop</P><P>'开始处理文件数据
Do while lenB(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
If strFileName<>"" Then
binFieldValue=MIDB( binHTTPHeader, DataStart, DataEnd )
'将上传的文件写入数据库
set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DSN=abc"
SQL="select from User_File"
set rs=server.CreateObject("ADODB.Recordset")
rs.Open sql,conn,3,3
rs.addnew
rs("UserID")=UserID
rs("FileContentType")=strFileContentType
rs("FileContent").AppendChunk binFieldValue
rs.update
rs.close
set rs=Nothing
conn.Close
set conn=Nothing
End if
binHTTPHeader=MIDB(binHTTPHeader,INSTRB( DataStart + 1, binHTTPHeader, divider ))
loop
%>
4。下载用户上传的文件
<%
Response.Buffer = true
Response.Clear
UserID=request("UserID")</P><P>Set conn=server.createobject("adodb.connection")
set rs=server.createobject("adodb.recordset")
conn.open "DSN=UploadFile"
rs.open "select from User_File where UserID='"&UserID&"'",conn,3,3
Response.ContentType = rs("FileContentType")</P><P>lngOffset=0
conChunkSize=1024
lngPictSize=rs("FileContent").ActualSize
Do While lngOffset < lngPictSize
varChunk = rs("FileContent").GetChunk(conChunkSize)
Response.BinaryWrite varChunk
lngOffset = lngOffset + conChunkSize
If lngOffset > lngPictSize Then Exit Do
Loop
rs.close
set rs=Nothing
conn.close
set conn=nothing
%></P><P>就是这些了,希望此方法对大家能有所帮助。)
1。数据库表结构(Aess)
UserID:Text(保存上传文件的用户ID)
FileContentType:Text(用来保存上传文件的类型,eg:"Application/msword",主要用来使用户能正确下载此文件)
FileContent:OLE Object(保存文件数据)
2。HTML文件
muploadfile.htm
<Form name="upload_file" enctype="multipart/form-data" action="muploadfile.asp" method=post>
<input type=hidden name="UserID" value="abc">
<input type=hidden name="FileUploadStart"> '这里用来表示开始文件数据上传
File to send:
<INPUT TYPE="file" name="file_up" size="30"><br>
<INPUT TYPE="file" name="file_up" size="30"><br>
<input type=hidden name="FileUploadEnd"> '这里用来表示文件数据结束
<input type=submit value=Submit>
</Form></P><P>
3。ASP文件
muploadfile.asp</P><P><%
Response.Expires=0
Function bin2str(binstr)
Dim varlen,clow,c,skipflag </P><P> skipflag=0
c = ""
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
c =c & Chr(AscW(MidB(binstr,i+1,1) & clow))
skipflag=1
Else
c = c & Chr(AscB(clow))
End If
Else
skipflag=0
End If
Next
End If
bin2str = c
End Function </P><P>
varByteCount = Request.TotalBytes
bnCRLF = chrB( 13 ) & chrB( 10 )
binHTTPHeader=Request.BinaryRead(varByteCount)
Divider = LEFTB( binHTTPHeader, INSTRB( binHTTPHeader, bnCRLF ) - 1 )</P><P>'开始读非文件域的数据
Do while lenB(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)
'判断文件数据时候开始
If strComp(strFieldName,"FileUploadStart",1)=0 Then
binHTTPHeader=MIDB(binHTTPHeader,INSTRB( DataStart + 1, binHTTPHeader, divider ))
exit do
End if
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> '非文件上传域变量赋值
execute strFieldName&"="""&strFieldValue&""""
binHTTPHeader=MIDB(binHTTPHeader,INSTRB( DataStart + 1, binHTTPHeader, divider ))
loop</P><P>'开始处理文件数据
Do while lenB(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
If strFileName<>"" Then
binFieldValue=MIDB( binHTTPHeader, DataStart, DataEnd )
'将上传的文件写入数据库
set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DSN=abc"
SQL="select from User_File"
set rs=server.CreateObject("ADODB.Recordset")
rs.Open sql,conn,3,3
rs.addnew
rs("UserID")=UserID
rs("FileContentType")=strFileContentType
rs("FileContent").AppendChunk binFieldValue
rs.update
rs.close
set rs=Nothing
conn.Close
set conn=Nothing
End if
binHTTPHeader=MIDB(binHTTPHeader,INSTRB( DataStart + 1, binHTTPHeader, divider ))
loop
%>
4。下载用户上传的文件
<%
Response.Buffer = true
Response.Clear
UserID=request("UserID")</P><P>Set conn=server.createobject("adodb.connection")
set rs=server.createobject("adodb.recordset")
conn.open "DSN=UploadFile"
rs.open "select from User_File where UserID='"&UserID&"'",conn,3,3
Response.ContentType = rs("FileContentType")</P><P>lngOffset=0
conChunkSize=1024
lngPictSize=rs("FileContent").ActualSize
Do While lngOffset < lngPictSize
varChunk = rs("FileContent").GetChunk(conChunkSize)
Response.BinaryWrite varChunk
lngOffset = lngOffset + conChunkSize
If lngOffset > lngPictSize Then Exit Do
Loop
rs.close
set rs=Nothing
conn.close
set conn=nothing
%></P><P>就是这些了,希望此方法对大家能有所帮助。)
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程