如何处理ASP中的图象

80酷酷网    80kuku.com

  在用asp编程中,很多时侯要用到图象。对于单纯从数据库中处理一个图象,方法大家讲了很多,也不难,可以看下面的代码:这里假设你有个数据库名字叫:pubs,在数据库中有一个叫:pub_info的表,在表中有一个logo的blob列。我们查出pub_id=0736的人的相片。
file&: showimg.asp
***************************************
<% language="vb" %>
<%
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 ost" 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=gb2312">
<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
%>
***************************

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: