![]() ![]() ![]() ![]() |
|||||
|
|||||
樓主 站務人員 站長 ![]()
![]() ![]() ![]() |
在用ASP編程中,很多時侯要用到圖像。對於單純從資料庫中處理一個圖像,方法大家講了很多,也不難, 可以看下面的代碼: 這裡假設你有個資料庫名字叫:PUBS,在資料庫中有一個叫:PUB_INFO的表,在表中有一個LOGO 的BLOB列。我們查出PUB_ID=0736的人的相片。 FILE: SHOWIMG.ASP *************************************** <%@ LANGUAGE="VBSCRIPT" %> <% ' Clear out the existing HTTP header information Response.Expires = 0 Response.Buffer = TRUE Response.Clear ' Change the HTTP header to reflect that an image is being passed. Response.ContentType = "image/gif" Set cn = Server.CreateObject("ADODB.Connection") ' The following open line assumes you have set up a System DataSource ' by the name of myDSN. cn.Open "DSN=myDSN;UID=sa;PWD=;DATABASE=pubs" Set rs = cn.Execute("SELECT logo FROM pub_info WHERE pub_id='0736'") Response.BinaryWrite rs("logo") Response.End %> ***************************************** 執行這個ASP文件就可以看到你存在資料庫中的圖像了。 但如果是同時處理文字和圖像就會有些困難了:-( 比如:一個企業的人員管理,後臺資料庫可以用SYBASE或SQL SERVER等。(我在這用SQL SERVER)當 你在企業內部需要用到BROWSE/SERVER方式,即用瀏覽器查看員工的個人訊息時,就即要處理文字訊息同時 還要用到關於圖像的技巧。 問題在於你顯示文字訊息時HTML的HEAD中的CONTENT=“TEXT/HTML”,而顯示圖像則必須是 CONTENT=“IMAGE/GIF”或者是CONTENT=”IMAGE/JPEG“。因此你是無法隻用一個ASP文件就把文字訊息和 圖像都處理完的,解決的辦法是:用一個單獨的ASP文件處理圖像,然後在處理文字訊息的ASP文件中調用 這個ASP文件。 在這給大家介紹一個我的解決方法,希望大家一起討論: 環境:WINNT4.0 SQL SERVER IIS3.0 資料庫名:RSDA 表名:RSDA_TABLE 目的:從RSDA_TABLE中查出ID=00001的人員的訊息,包括姓名,年齡和照片 第一步:創建一個查詢表單RSDA.HTM: ********************************** <html> <head> </head> <body> <form method="POST" action="SEARCH.ASP"> <p>請輸入編號:<input type="text" name="T1" size="20"><input type="submit" value="提交" name="B1"><input type="reset" value="復原" name="B2"></p> </form> </body> </html> *********************************** 第二步:建立SEARCH.ASP *********************************** <html> <head> <meta http-equiv="content-type" content="text/html;charset=big5"> <title>查詢結果</title> </head> <body bgColor=Azure> <% session("RSDA_ID")=Request.Form("T1") '這裡我用了一個SESSION變量,是為了在處理圖像的ASP文件中再次調用 temp_id=session("RSDA_ID") <font size=4 color=OrangeRed> 查詢結果:</font> <%set conntemp=server.createobject("adodb.connection") conntemp.open "dsn=RSDA;uid=sa;pwd=SA" set rstemp=conntemp.execute("select * from RSDA_TABLE where rsda='"&temp_id&"'") %> <% 'put headings on the table of field names nobody="對不起!在我們的資料庫裡沒有您要找的資料!"%> '判斷是否有這個人 <%if rstemp.eof then %> <font size="5" color=OrangeRed> <%Response.Write(nobody)%></font> <%else%> <div align="center"> <center> <table border="1" width="73%" height="399"> <tr> <td width="21%" height="49" align="center"><p align="center">姓 名</td> <td width="30%" height="49" align="center"> <font size=4 color=OrangeRed><%=rstemp(0)%></font></td> </td> <tr> <td width="21%" height="47"><p align="center">年 齡</td> <td width="30%" height="47" align="center"> <font size=4 color=OrangeRed><%=rstemp(0)%></font></td> </tr> <tr> <td width="49%" height="146" rowspan="3" colspan="2"> <img src="jpg.asp"></td> 'JPG.ASP就是我們將要建立的專門處理圖像的ASP文件 </tr> </table> </center></div> rstemp.close set rstemp=nothing conntemp.close set conntemp=nothing %> </BODY> </HTML> *********************************** 第三步:建立處理圖像的ASP文件。(JPG.ASP) *********************************** <% Response.Expires = 0 Response.Buffer = TRUE Response.Clear ' Open database Set conntemp = Server.CreateObject("ADODB.Connection") conntemp.open "dsn=RSDA;uid=sa;pwd=SA" 'change http header Response.ContentType = "image/jpeg" ' or "IMAGE/GIF" ' Get picture TEMP_ID=session("RSDA_ID") Set Rs = conntemp.Execute("SELECT photo from RSDA_table where ID='"&TEMP_ID&"'") Response.BinaryWrite Rs("photo") Session.Abandon Response.End %> ********************************** 這裡主要就是用到了一個小技巧就是利用了一個SESSION變量來實現兩次同條件查詢。 大家如我上述隻需少量改動,就可以實現一個頁面既有文字又有圖像了! --------------------------------------------------------------------------------
本篇文章發表於2002-06-09 00:00
|