|
以下代碼沒有規(guī)范,還有些功能沒完成,有那位高手能把它修改一下,封裝成類則更是造福大眾,或者以后有時(shí)間我會(huì)做的。有任何錯(cuò)誤或建議請(qǐng)一定要給我發(fā)E-mail:sobina@21cn.com,謝謝。 好了,少說多做,本示例在w2kServer,IIS5,SQL SERVER7中測(cè)試通過。 如有不明白的可到精華區(qū)查“圖象”或“圖片”關(guān)鍵字找到答案,或?qū)懶沤o我。 示例一共有三個(gè)文件:upload.htm(上傳界面) process.asp(處理程序) showimg.asp(顯示圖象) 數(shù)據(jù)庫(kù):在pubs數(shù)據(jù)庫(kù)中建立一個(gè)新表名為imgtest 字段名 類型 長(zhǎng)度 ----------------------------------------------------- idint (自動(dòng)編號(hào)) img iamge(Access為OLE) imginfo nchar50
以下是三個(gè)文件的代碼:
upload.htm --------------------------------------------------------------------- <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <style type="text/css"> <!-- td {font-size: 9pt} a {color: #000000; text-decoration: none} a:hover {text-decoration: underline} .tx {height: 16px; width: 30px; border-color: black black #000000; border-top-width: 0px;
border-right-width: 0px; border-bottom-width: 1px; border-left-width: 0px; font-size: 9pt;
background-color: #eeeeee; color: #0000FF} .bt {font-size: 9pt; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px;
border-left-width: 0px; height: 16px; width: 80px; background-color: #eeeeee; cursor: hand} .tx1 { height: 20px; width: 30px; font-size: 9pt; border: 1px solid; border-color: black black
#000000; color: #0000FF} --> </style> </head>
<body bgcolor="#FFFFFF" text="#000000"> <form name="form1" method="post" action="process.asp" enctype="multipart/form-data" > <table width="71%" border="1" cellspacing="0" cellpadding="5" align="center"
bordercolordark="#CCCCCC" bordercolorlight="#000000"> <tr bgcolor="#CCCCCC"> <td height="22" align="left" valign="middle" bgcolor="#CCCCCC"> Sobina 的圖文上傳界面</td> </tr> <tr align="left" valign="middle" bgcolor="#eeeeee"> <td bgcolor="#eeeeee"> <br> </td> </tr> <tr align="center" valign="middle"> <td align="left" id="upid" height="122"> <p>圖象路徑: <input type="file" name="img" style="width:400" class="tx1" value=""> </p> <p>圖象說明: <input type="text" name="imginfo"> </p> </td> </tr> <tr align="center" valign="middle" bgcolor="#eeeeee"> <td bgcolor="#eeeeee" height="2"> <input type="submit" name="Submit" value="· 提交 ·" class="bt"> <input type="reset" name="Submit2" value="· 重置 ·" class="bt"> </td> </tr>
</form> </body> </html> ------------------------------------------------------------------------ process.asp ------------------------------------------------------------------------ <!--#include file="../bin/strCnn.asp"--> <% response.expires=0 '目的:將二進(jìn)制字符轉(zhuǎn)換成普通字符 Function bin2str(binstr) Dim varlen,clow,ccc,skipflag skipflag=0 ccc = "" 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 bin2str = ccc End Function
'目的:把表單中的圖象數(shù)據(jù)分離出來(lái) '其中參數(shù)formsize為表單數(shù)據(jù)大小,formdata為表單的總數(shù)據(jù) Function ImageUp(formsize,formdata) bncrlf=chrb(13) & chrb(10) divider=leftb(formdata,instrb(formdata,bncrlf)-1) 'formdata第一個(gè)bncrlf左邊的數(shù)據(jù) datastart=instrb(formdata,bncrlf & bncrlf)+4'兩個(gè)bncrlf右邊的數(shù)據(jù)的起始位 dataend=instrb(datastart+1,formdata,divider)-datastart Imageup=midb(formdata,datastart,dataend) End Function
'目的:把表單中的變量值取出 '其中參數(shù)varName為要尋找的字段變量,strTXT為已從圖象中分離出來(lái)的的所有文本 Function findVar(varName,strTxt) startPos=1 strLen=len(varName)+2 '表單中可能有多個(gè)同名變量(用在有主表與明細(xì)表中的數(shù)據(jù)更新中) for i=1 to len(strTXT) varStart=instr(startPos,strTXT,varName)+strLen+3 varEnd=instr(varStart,strTXT,"--")-2 varValLen=varEnd-varStart
inVar=mid(strTXT,varStart,varValLen) findVar=findVar & inVar
startPos=instr(varStart,strTXT,varName) if startPos=0 then exit for'如果找不到則退出循環(huán) findVar=findVar & ","' 以","作為多個(gè)同名變量值間的分隔符,實(shí)際上本例倒不需要 next
End function FormSize = Request.TotalBytes FormData = Request.BinaryRead( FormSize ) Image = ImageUp(FormSize,Formdata)
'以下兩步不能省略,否則取不出文字 strTXT=mid(formdata,instr(formdata,image)+len(image)+1) '取出文字 strTXT=bin2str(strTXT)'二進(jìn)制轉(zhuǎn)換成普通文字
imginfo=findVar("imginfo",strTXT)'相當(dāng)于request.form("imginfo") 'MyArray = Split(imginfo, ",", -1, 1)'分離從imginfo返回的多個(gè)值,在本例子中不用
Application.Lock set objCnn=Server.CreateObject("ADODB.Connection") objCnn.Open strCnn set rec=Server.CreateObject("ADODB.Recordset") rec.Open "imgtest",objCnn,1,3 rec.addnew '如果你只想更新一個(gè)記錄,則可使這句失效 rec("imginfo")=imginfo if Len(Image)>1 then rec("img").Appendchunk Image '把圖象寫入數(shù)據(jù)庫(kù) end if rec.update rec.close:set rec=nothing set objCnn=Nothing Application.Unlock '輸出圖象說明和圖象 response.write imginfo response.write "<img src='showimg.asp?imginfo=" & imginfo & "' boder=0>" %> ----------------------------------------------------------------------- showimg.asp ----------------------------------------------------------------------- <!--#include file="../bin/strCnn.asp"--> <% response.expires=0 response.buffer=true response.clear Response.ContentType = "image/*"
set objCnn=server.CreateObject("ADODB.connection") objCnn.Open strCnn
strsql="SELECT * FROM imgtest where imginfo='" & Request.QueryString("imginfo") & "'" set rec=objCnn.Execute(strsql)
Response.BinaryWrite rec("img")
rec.close:set rec=nothing set objCnn=nothing %>
需要提醒的是: Access數(shù)據(jù)庫(kù)要把顯示圖象的showimg.asp文件作相應(yīng)調(diào)整 showimg.asp ----------------------------------------------------------------------- <!--#include file="../bin/strCnn.asp"--> <% response.expires=0 response.buffer=true response.clear Response.ContentType = "image/bmp"
set objCnn=server.CreateObject("ADODB.connection") objCnn.Open strCnn
strsql="SELECT * FROM imgtest where imginfo='" & Request.QueryString("imginfo") & "'" set rec=objCnn.Execute(strsql)
Dim intSize,objOle,realSize Const cntHeader=76'Access數(shù)據(jù)OLE開頭多了76 bytes intize=rec("img").ActualSize objOle=rec("img").GetChunk(cntHeader)'76 bytes realSize=rec("img").GetChunk(intFldSize-cntHeader)'獲取圖像的真正bytes
Response.BinaryWrite relSize'輸出圖象
rec.close:set rec=nothing set objCnn=nothing %>
|