–
ASP將圖片上傳到數(shù)據(jù)庫的代碼
<%
formsize=request.totalbytes
formdata=request.binaryread(formsize)
bncrlf=chrB(13)&chrB(10)
divider=leftB(formdata,clng(instrb(formdata,bncrlf))-1)
datastart=instrb(formdata,bncrlf&bncrlf)+4
dataend=instrb(datastart+1,formdata,divider)-datastart
mydata=midb(formdata,datastart,dataend)
sql=”select * from pic”
getdata(sql)
rs.addnew
rs(“img”).AppendChunk myData
rs.update
rsclose()
response.clear
response.write “success!”
end if
%> <%
‘這是一段將圖片上傳到數(shù)據(jù)庫的代碼
formsize=request.totalbytes
‘獲取請(qǐng)求中包括的總字節(jié)數(shù)
formdata=request.binaryread(formsize)
‘保存從客戶端讀取到的二進(jìn)制數(shù)據(jù)
bncrlf=chrB(13)&chrB(10)
divider=leftB(formdata,clng(instrb(formdata,bncrlf))-1)
‘分隔標(biāo)志串
‘數(shù)據(jù)長度,減1是因?yàn)閿?shù)據(jù)文件的存取字節(jié)數(shù)問題(可能是AppendChunk方法的問題):
‘由于字節(jié)數(shù)為奇數(shù)的圖象存到數(shù)據(jù)庫時(shí)會(huì)去掉最后一個(gè)字符導(dǎo)致圖象不能正確顯示,
‘字節(jié)數(shù)為偶數(shù)的數(shù)據(jù)文件就不會(huì)出現(xiàn)這個(gè)問題,因此必須保持字節(jié)數(shù)為偶數(shù)。
datastart=instrb(formdata,bncrlf&bncrlf)+4
‘分隔標(biāo)志串長度
dataend=instrb(datastart+1,formdata,divider)-datastart
‘減去分隔標(biāo)志串長度
mydata=midb(formdata,datastart,dataend)
‘獲取最終數(shù)據(jù)
sql=”select * from pic”
getdata(sql)
‘getdata為自定義函數(shù),連接、打開數(shù)據(jù)庫、并運(yùn)行sql語句
rs.addnew
‘添加新的數(shù)據(jù)
rs(“img”).AppendChunk myData
‘使用AppendChunk方法添加數(shù)據(jù)
rs.update
‘更新數(shù)據(jù)庫
rs.close()
‘關(guān)閉數(shù)據(jù)庫
response.clear
‘清除緩沖區(qū)中的所有 HTML 輸出
response.write “success!”
‘顯示成功信息
end if
‘最前面應(yīng)該有if…then語句
%>