多文件多文本框上传程序接触ASP也有将近一年时间,总想说一下自己的心得。正好今天单位工作不忙,我把我自己编写的多文件多文本框同时提交到服务器的纯ASP上传程序整理了一下,发表出来。希望与大家共同交流。 程序分为三部分: 1、储存文件路径、文件说明、上传文件个数的一个数据库aaa.mdb,在这里我放在应用目录里mydatabase文件夹里 2、上传程序界面 gjimg.asp 3、纯ASP上传主程序 khimg.asp 上传文件存放在upload文件夹里 +==================================================== 数据库aaa.mdb 表TPIC 字段 类型 ID 自动 PICPATH 字段 储存文件的在服务器的绝对路径 PICCOU 字段 储存这次上传的第几个文件 PICTXT 字段 储存文件的文字说明
表的每一行储存一个文件和它的文字说明 +===================================================== 以下代码是上传界面 gjimg.asp 申明一下:这个界面是我从一个叫“红涯”的网友那里得到,不过她没有提供上传代码,我根据她的界面编写了上传主程序。 ------------------------------------------------------------------- <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>投稿图片上传</title> <script language="JavaScript"> function setid() { if(!window.form11.imgcou.value) window.form11.imgcou.value=1; aa=window.form11.imgcou.value; if (aa>100) //限制为100个文件 {aa=100; window.form11.imgcou.value=100; } str1='' for(j=1;j<=aa;j++) str1+='<br> <font color="#003333" size="2" id="shuang">图片'+j+':</font><input name="file'+j+' " type="file" class="xian" style="height:20;width:221;"><br> <font color="#003333" size="2">图片说明'+j+':</font> <input name="txt'+j+'" type="text" class="xian" style="height:20;width:200;"> <br><br>'; window.imgid.innerHTML=str1; }
</script> </head>
<body > <center> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td height="25" colspan="2" align="center"><font color="#FF0000" size="2"><%=session("errmess11")%></font></td> </tr> <tr> <td width="67%" height="25" align="right" ><font color="#003333" size="2">请输入上传图片的张数:</font> </td> <td width="33%" height="25" align="left" > <form method="post" action="" name="form11" id="form11"> <input name="imgcou" id="imgcou" type="text" style="height:20;width:20;" class="xian"> <input name="tsd" type="button" value="设定" style="height:20;width:50;" class="xian" onClick="setid();"> </form></td> </tr> <form name="SCM" action="khimg.asp" method="post" enctype="multipart/form-data"> <tr> <td colspan="2" align="center" id="imgid"><br> <font color="#003333" size="2" id="shuang">图片1:</font> <input name="file1" type="file" class="xian" style="height:20;width:221;"> <br> <font color="#003333" size="2">图片说明1:</font> <input name="txt1" type="text" class="xian" style="height:20;width:200;"> <br></td> </tr> <tr> <td align="center" height="30" ></td> <td align="left" ><input type="submit" name="FMDJ" value="提交" style="height:20;width:40;" class="xian"> <input type="reset" name="Submit2" value="重设" style="height:20;width:40;" class="xian"></td> </tr> </form> </table> </center> </body> </html>
+======================================================================== 以下代码是纯ASP上传主程序 khimg.asp 我已经在重要的地方都加了注释了。 ------------------------------------------------------------------------- <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <%response.Buffer=true response.Clear() dim tfilestart,tfilesize,ttxt,errno,tid,tfileext dim ulrequest errno=0 set ulrequest=createobject("scripting.dictionary") bytecount=request.TotalBytes requestBin=request.BinaryRead(bytecount) "读取表单所有二进制数据 buildUploadRequest requestBin "调用上传主函数 buildUploadRequest set readfile=createobject("adodb.stream") readfile.mode=3 readfile.type=1 readfile.open readfile.write requestBin "把读取的二进制数据再存入一个adodb.stream对象中 select case errno case 0 ''没有发生错误的情况下把文件存进指定目录,文件路径、文本、文件数量写入数据库, connstr="dbq= "&server.MapPath("mydatabase\aaa.mdb")&"; driver={Microsoft Access Driver (*.mdb)}" set conn=server.CreateObject("adodb.connection") conn.open connstr set rec=server.CreateObject("adodb.recordset") sqlstr="select * from TPIC " rec.open sqlstr,conn,2,3 ''打开数据库 ulkey=ulrequest.keys ''取出数据字典ulrequest的键 set addfile=createobject("adodb.stream") addfile.mode=3 addfile.type=1''再建立一个adodb.stream对象,以便拷贝readfile对象的文件数据。 aa=ubound(ulkey)''取键的个数,个数代表这有文件和文本的总数。 cou=0 for i=0 to aa "每个循环写存入一个文件或一个文本写入数据库 addfile.open if left(trim(ulkey(i)),4)="file" then "是文件数据 titem=ulkey(i) tfilesize=ulrequest.item(titem).item("filesize") tfilestart=ulrequest.item(titem).item("filestart") tfileext=ulrequest.item(titem).item("fileext") ''取得文件数据在 readfile中的位置,以及扩展名 readfile.position=tfilestart-1 readfile.copyto addfile,tfilesize ''把 readfile里的文件数据拷贝入addfile filepath=server.MapPath("upload") filename=year(now())&month(now())&day(now())&hour(now())&minute(now())&second(now())&i&tfileext filepath=filepath&"\"&filename"给文件命名,我这里是用时间给文件命名。 vfilepath="upload\"&filename addfile.savetofile filepath ''文件存入upload文件夹 cou=cou+1 rec.addnew rec.fields("PICPATH")=vfilepath rec.fields("PICCOU")=cou "数据库写入文件信息以及是第几个文件 elseif left(trim(ulkey(i)),3)="txt" then "是文本 titem=ulkey(i) ttxt=ulrequest.item(titem).item("myValue") rec.fields("PICTXT")=ttxt "取文本存入数据库。 rec.update end if addfile.close next set addfile=nothing set readfile=nothing rec.close conn.close set rec=nothing set conn=nothing session("errmess22")="图片上传成功!" response.Redirect("upimg.asp") case 1 readfile.close set readfile=nothing session("errmess11")="上传类型不正确!" response.Redirect("gjimg.asp") case 2 readfile.close set readfile=nothing session("errmess11")="上传的文件中有未选图片的!" response.Redirect("gjimg.asp") case 3 readfile.close set readfile=nothing session("errmess11")="上传的图片中,有图片超过200k了" response.Redirect("upimg.asp") end select %> <!--文件上传主函数--> <%sub buildUploadRequest(requestBin) dim fileext,filesize,filestart posbeg=1 posend=instrB(posbeg,requestBin,getByteString(chr(13))) boundary=midB(requestBin,posbeg,posend-posbeg) boundarypos=instrB(1,requestBin,boundary) boundarypos=clng(boundarypos)"取上传数据分隔符和起始点 do until (boundarypos=instrB(requestBin,boundary&getByteString("--"))) dim uploadcontrol set uploadcontrol=createobject("scripting.dictionary") "建立数据字典 pos=instrB(boundarypos,requestBin,getByteString("Content-Disposition")) pos=instrB(pos,requestBin,getByteString("name=")) posbeg=pos+6 posend=instrB(posbeg,requestBin,getByteString(chr(34))) myname=getString(midB(requestBin,posbeg,posend-posbeg))"取得上传表单的名字 posfile=instrB(boundarypos,requestBin,getByteString("filename=")) posbound=instrB(posend,requestBin,boundary) if posfile<>0 and (posbound>posfile) then"如果是文件 posbeg=posfile+10 posend=instrB(posbeg,requestBin,getByteString(chr(34))) filename=getString(midB(requestBin,posbeg,posend-posbeg))''取得文件名 fileext=right(trim(filename),4) "文件扩展名 uploadcontrol.add "fileext",fileext "文件名写入数据字典 if fileext<>".jpg" and fileext<>".gif" and fileext<>".GIF" AND fileext<>".JPG" THEN "这里是限制 上传文件类型,这里把文件类型限制为GIF、JPG errno=1 END IF uploadcontrol.add "filename",filename pos=instrB(posend,requestBin,getByteString("Content-Type:")) posbeg=pos+14 posend=instrB(posbeg,requestBin,getByteString(chr(13))) ctype=getString(midB(requestBin,posbeg,posend-posbeg)) uploadcontrol.add "Ctype",ctype posbeg=posend+4 posend=instrB(posbeg,requestBin,boundary)-2 myValue="" filestart=posbeg filesize=posend-posbeg "得到文件数据在readfile里的起始点和大小 if filesize<=0 then "判断文件大小是否为0,既没有文件上传。 errno=2 end if if filesize>cint(200)*1024 then "限制文件大小,我这里限制为200K。 errno=3 end if uploadcontrol.add "filestart",filestart uploadcontrol.add "filesize",filesize "文件数据的大小和起始点分别写入uploadcontrol数据字典的filestart、filesize项 ELSE ''如果是文本框文字 pos=instrB(pos,requestBin,getByteString(chr(13))) posbeg=pos+4 posend=instrB(posbeg,requestBin,boundary)-2 if posbeg<posend then myValue=getString(midB(requestBin,posbeg,posend-posbeg)) else myValue="" end if end if uploadcontrol.add "myValue",myValue "把文本存入uploadcontrol的"myValue"项 ulrequest.add myname,uploadcontrol "把uploadcontrol再存入ulrequest的myname项,myname是上传表单的某一张表单的名字。 boundarypos=instrB(boundarypos+lenB(boundary),requestBin,boundary) loop end sub ''取字节函数 function getByteString(StringStr) for i=1 to len(StringStr) char=mid(StringStr,i,1) getByteString=getByteString&chrB(ascB(char)) next end function ''取字符函数 function getString(StringBin) for i=1 to lenB(StringBin) bin=midB(StringBin,i,1) if ascB(bin)>127 then''处理汉字 getString=getString&chr(ascW(midb(StringBin,i+1,1)&bin)) i=i+1 else getString=getString&chr(ascB(bin)) end if NEXT END FUNCTION %> +=================================================== 以上程序代码在自己的个人网站中调试通过。 希望大家提出更好的建议!
|