欢迎光临
我们一直在努力

Asp多表单域无组件文件上传的例子-ASP教程,组件开发

建站超值云服务器,限时71元/月

最近经常有人问到这类问题,在此转贴一下,内容:
1。数据库表结构(access):
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,ccc,skipflag </p><p>    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
                    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 </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>就是这些了,希望此方法对大家能有所帮助。:)

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Asp多表单域无组件文件上传的例子-ASP教程,组件开发
分享到: 更多 (0)