當前位置:首頁 » 操作系統 » asp圖片資料庫

asp圖片資料庫

發布時間: 2023-08-27 02:25:23

⑴ 在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

  • %>

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

⑵ asp如何實現表單上傳圖片後存入access資料庫

asp中表單上傳圖片後會解析成二進制byte數組保存到access資料庫。
1、上傳圖片:

OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
pictureBox1.Image = new Bitmap(open.FileName);
// image file path
textBox1.Text = open.FileName;
}
2、保存圖片信息到acess資料庫。
C#實現,保存核心代碼:

var pic = File.ReadAllBytes(yourFileName);
using(OleDbConnection con = new OleDbConnection(constr))
using(OleDbCommand cmd = new OleDbCommand("Insert Into DML_Books_List(ID, [Image]) values (@id, @image)", con))
{
con.Open();
cmd.Parameters.AddWithValue("@id", TextBox1.Text);
cmd.Parameters.AddWithValue("@image", pic);
cmd.ExecuteNonQuery();
}

⑶ ASP如何實現批量上傳圖片,並在資料庫中有記錄

<%

dim conn

dim rs

dim rs2


set conn=server.createobject("adodb.connection")

conn.connectionstring="Provider = Microsoft.Jet.OLEDB.4.0;Data Source="&server.mapPath("db.mdb")

conn.open


formsize=request.totalbytes


if formsize<>0 then '這里只判斷了是否等於0,等於0就說明沒有傳過來數據。以後可以要多做點判斷,比如限制圖片大小的時候要判斷一下。

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)'這里也要檢測一下,就是檢測這個數據裡面是不是有病毒詞彙之類的,這個可以在網上查一下病毒裡面可能出現的詞語,如果有這些詞語,直接在這里就把這個mydata銷毀,然後response.redirect "xxx.asp",有病毒趕快跳轉,不要處理帶病毒的數據。


set rs=server.createobject("ADODB.recordset")

sql="select * from imgurl"

rs.open sql,conn,1,3

rs.addnew

rs("img").AppendChunk myData'把這個圖片的二進制數據新增到img欄位裡面。

tpm=replace(now(),"/","_") '用的日期做名字,日期裡面有"/"," ",":"之類的東西,這些東西全部替換成"_"。

tpm=replace(tpm,":","_")

tpm=replace(tpm," ","_")

rs("圖片名")=tpm

rs.update


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

MyStream.Type=1

MyStream.Open

MyStream.Write rs("img").getChunk(8000000)

MyStream.SaveToFile server.mappath("pic/"&tpm&".jpg")

MyStream.close

set MyStream=nothing


rs("img")="" '把access裡面的二進制圖片內容刪除,只保留圖片的名字。

rs.update


rs.close

set rs=nothing


response.write "<script>alert(""上傳成功"");location.href=""upload.asp"";</script>"

end if

%>


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

<input type="file" name="imgurl">

<input type="submit" name="ok" value="上傳">

</form>



<!--------------下面是把圖片顯示出來------------------->



<div style="margin-top:2em;"><!--------------創建一個div把圖片顯示區域定位一下------------------->

<%

set rs2=server.createobject("ADODB.recordset")

sql2="select * from imgurl order by id desc"

rs2.open sql2,conn,1,1

for s=1 to rs2.recordcount

if not rs2.eof and not rs2.bof then

%>

<img src="pic/<%=rs2("圖片名")%>.jpg">

<%

else

exit for

end if

rs2.movenext

next

rs2.close

set rs2=nothing

%>

</div>

<%

conn.Close

Set conn = Nothing

%>


<!--至於刪除圖片,這個就很簡單了,圖片的名字都已經進access裡面了,直接讀一下access裡面的名字,讀了一個名字,然後用fso在pic文件夾裡面去刪除對應的圖片文件就可以了。--->

效果圖:網頁鏈接

熱點內容
黑蘋果idea編譯慢 發布:2025-02-01 13:45:30 瀏覽:548
c和linux 發布:2025-02-01 13:39:38 瀏覽:175
android實現列表 發布:2025-02-01 13:38:06 瀏覽:102
安卓手機為什麼會出現橫條 發布:2025-02-01 13:33:38 瀏覽:625
jqueryajax提交文件上傳 發布:2025-02-01 13:32:03 瀏覽:689
沒連上wifi如何修改wifi密碼 發布:2025-02-01 13:20:42 瀏覽:758
絕對賽車3安卓在哪裡下載 發布:2025-02-01 12:42:30 瀏覽:718
mysql修改資料庫字元 發布:2025-02-01 12:37:52 瀏覽:570
阿里雲伺服器出廠密碼是多少 發布:2025-02-01 12:35:43 瀏覽:668
手機傳文件伺服器和ip地址 發布:2025-02-01 12:15:01 瀏覽:660