如何处理ASP中的图象

王朝asp·作者佚名  2006-12-16
窄屏简体版  字體: |||超大  

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

%>

**********************************

这里主要就是用到了一个小技巧就是利用了一个session变量来实现两次同条件查询。大家如我上述只需少量改动,就可以实现一个页面既有文字又有图象了!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航