多文件多文本框上傳程序接觸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 %> +=================================================== 以上程序代碼在自己的個人網站中調試通過。 希望大家提出更好的建議!
|