當前位置:編程學習大全網 - 源碼下載 - 在ASP中,如何把圖片和文字同時保存到數據庫中,並在需要時同時輸出。

在ASP中,如何把圖片和文字同時保存到數據庫中,並在需要時同時輸出。

假設有這樣壹個表單。

<form id="myform" enctype="multipart/form-data" action="upload.asp" method="post">

<input type="file" name="file1"><!-用於提交文件,既圖片文件-->

<input type="text" name="mytext"><!-用於提交文字信息-->

<input type="submit" name="tj" value="提交"><!--表單提交按鈕-->

</form>

表單能提交圖片,也能提交文字。內容提交到upload.asp去處理。下面是upload.asp裏面的部分內容。

<%Response.Charset="utf-8"%>

<%

'查詢字符串thisstr2在字符串thisstr1裏面第N次出現的位置,如果沒有出現,返回空。

'這個函數也很有用。比如獲得文件名的時候,文件名 name="file1" ?filename="abc.jpg",先二進制轉文本,然後找到雙引號第三次出現的位置和第四次出現的位置,兩個位置中間的內容就是文件名,還可以得到文件的擴展名"jpg"

function strN(thisN,thisstr1,thisstr2)

thistemp=1

for thiss=1 to len(thisstr1)

thisdatastart=instr(thistemp,thisstr1,thisstr2)

if thisdatastart=0 or thisdatastart=null then

exit for

end if

if thisdatastart<>0 or thisdatastart<>null then

thistemp=thisdatastart+len(thisstr2)

thiscishu=thiscishu+1

if thiscishu=thisN then

strN=thisdatastart

exit for

end if

end if

next

if thiscishu<thisN then

strN=""

end if

end function

'查詢二進制數據流thisstr2在thisstr1裏面出現的次數。這個函數在已知表單提交信息條數的情況下就用不到。但如果是表單提交的信息條數未知,比如批量上傳圖片的時候,不知道有多少個type="file"的input,就需要用這個函數先判斷壹下。既判斷分割符在提交數據裏面出現的次數。出現了n次則有n-1條數據提交。

function mynumberb(thisstr1,thisstr2)

thistemp=1

for thisn=1 to len(thisstr1)

thisdatastart=instrb(thistemp,thisstr1,thisstr2)

if thisdatastart=0 or thisdatastart=null then

exit for

end if

if thisdatastart<>0 or thisdatastart<>null then

thistemp=thisdatastart+len(thisstr2)

thiscishu=thiscishu+1

end if

next

mynumberb=thiscishu

end function

'查詢二進制數據流thisstr2在thisstr1裏面第thisN次出現的位置,如果沒有出現,返回空。

'這個函數很有用,比如表單傳過來的數據都是用回車換行符號隔開的。只需要查詢回車換行符號第4次出現的位置和第五次出現的位置,就能找到文件二進制數據開始和結束的位置。如果表單發送過來的是文本信息,只需要找到回車換行符號第三次出現的位置和第四次出現的位置,就能找到文本的二進制數據。然後二進制轉文本,就提取出文本內容了。

function strNb(thisN,thisstr1,thisstr2)

thistemp=1

for thiss=1 to len(thisstr1)

thisdatastart=instrb(thistemp,thisstr1,thisstr2)

if thisdatastart=0 or thisdatastart=null then

exit for

end if

if thisdatastart<>0 or thisdatastart<>null then

thistemp=thisdatastart+len(thisstr2)

thiscishu=thiscishu+1

if thiscishu=thisN then

strNb=thisdatastart

exit for

end if

end if

next

if thiscishu<thisN then

strNb=""

end if

end function

'二進制轉文本

Function stb(vin)

const adTypeText=2

dim BytesStream,StringReturn

Set BytesStream=Server.CreateObject("ADODB.Stream")

with BytesStream

BytesStream.Type=adTypeText

BytesStream.Open

BytesStream.WriteText vin

BytesStream.Position=0

BytesStream.Charset="utf-8"

BytesStream.Position=2

StringReturn=BytesStream.ReadText

BytesStream.Close

end with

set BytesStream=Nothing

stb=StringReturn

end function

'以上幾個函數介紹完畢。接下來就是實際處理表單提交的信息了。

response.buffer=true

formsize=request.totalbytes

formdata=request.binaryread(formsize)

hcf=chrB(13)&chrB(10)'回車換行符號

fgf=leftB(formdata,clng(instrb(formdata,hcf))-1)'分隔符

cd=lenb(fgf)'分割符的長度

'截取第壹條數據,既文件數據。

mydatastart=strnb(1,formdata,fgf)+cd

mydataend=strnb(2,formdata,fgf)-1

mydatasize=mydataend-mydatastart+1

formdata1=midb(formdata,mydatastart,mydatasize)'第壹條提交的數據信息,既第壹個type=file的圖片文件

'得到文件的名字

mytempdata=stb(formdata1)

mydatastart=strn(3,mytempdata,"""")+1'雙引號第三次出現的位置加1就是文件名出現的開始位置

mydataend=strn(4,mytempdata,"""")-1'雙引號第四次出現的位置就是文件名結束的位置

mydatasize=mydataend-mydatastart+1

wjfilename=mid(mytempdata,mydatastart,mydatasize)'得到文件名字,就是提交的那個圖片的名字,比如"myimg.jpg"

'截取圖片文件的二進制數據

mydatastart=strnb(4,formdata1,hcf)+2'回車符號第四次出現的位置加2就是圖片文件的二進制數據開始的位置

mydataend=strnb(5,formdata1,hcf)-1'回車符號第五次出現的位置減1就是圖片二進制數據結束的位置

mydatasize=mydataend-mydatastart+1'圖片文件二進制數據的長度

wjdata=midb(formdata1,mydatastart,mydatasize)'得到圖片文件的二進制數據

'截取第二條數據,既截取提交的文本二進制數據

mydatastart=strnb(2,formdata,fgf)+cd

mydataend=strnb(3,formdata,fgf)-1

mydatasize=mydataend-mydatastart+1

formdata2=midb(formdata,mydatastart,mydatasize)'第二條提交的數據信息,既提交的文字信息。

'提取文本

mydatastart=strnb(3,formdata2,hcf)+2

mydataend=strnb(4,formdata2,hcf)-1

mydatasize=mydataend-mydatastart+1

wbdata=midb(formdata2,mydatastart,mydatasize)

wb=stb(wbdata)

'到此,表單信息全部接收完畢。

'wjfilename:文件名。

'wjdata:文件二進制數據。

'wb:文字信息。

'下面要做的就是把文本信息存入數據庫。把文件的二進制數據轉換成圖片存入文件夾,也可以直接二進制數據存放到數據庫裏面。

'至於怎麽存放路徑等壹系列問題,這些都是簡單問題。最難啃的骨頭已經啃完了。

'文件信息存入文件夾提供壹種思路,這種思路比較簡單。

'access裏面的temp字段是存儲二進制數據的,表名也叫temp

'call conn_open(conn,"xxx.mdb")打開access

'sql="select * from temp"

'call rs_open3(rs,sql)打開表

' rs.addnew

' rs("temp").appendchunk wjdata

' rs.update

' Set MyStream=Server.CreateObject("Adodb.Stream")

' MyStream.Type=1

' MyStream.Open

' MyStream.Write rs("temp").getChunk(8000000) 把數據庫裏面的圖片讀出來

'得到圖片上傳的日期時間連接1到1000之間的隨機數做圖片的名字,把時間裏面的左斜杠,冒號以及空格都替換掉。

' picName=replace(now(),"/","")

' picName=replace(picName,":","")

' Randomize

' picName=replace(picName," ","")&Int((1000 * Rnd) + 1)

' MyStream.SaveToFile server.mappath("img/"&picName&".jpg")

'把圖片存入目錄,註意,這裏如果事先前端做了判'斷用戶提交的圖片就必定是"jpg"格式,所以可以直接用。如果前端沒做判斷,就用剛'才得到的split(wjfilename,".")(1)作為擴展名。這玩意兒能看懂吧,wjfilename裏面保存的是圖片名字"abcd.jpg",用"."分割,

'後面的那個就是"jpg"圖片擴展名

' MyStream.close

' set MyStream=nothing

'call rs_close(rs)關閉表

'call conn_close(conn)關閉access

%>

以上提供的思路既然可以圖片文字壹起提交,也可以在不知道表單提交數據條數的情況下批量混合提交圖片和文字信息。原理是死的,人的思路是活的。活學活用最好。

  • 上一篇:超級兔子的系統保護好嗎?還需要其他的殺毒軟件嗎?
  • 下一篇:如何利用cpi計算實際價格
  • copyright 2024編程學習大全網