关于本站
管理团队
  
胡杨林-山东IT·互动主题区  [登录] [注册] [发表新文章]  

作者: ¤漂流瓶 收藏:0 回复:2 点击:5154 发表时间: 2004.01.10 10:33:42

如何处理ASP中的图象    


  
  在用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变量来实现两次同条件查询。大家如我上述只需少量改动,就可以实现一个页面既有文字又有图象了!
  
  

------------------------
〥^囧^〥shell〥^囧^〥贝贝〥^囧^〥

原创    收   藏  

回帖


回复人: 小鱼儿 Re:如何处理ASP中的图象     回复时间: 2004.01.16 20:31

    贝贝,你什么时候帮我弄一下文集里的图象啊..我好笨的

回    复    

回复人: 我·孤独 Re:如何处理ASP中的图象     回复时间: 2004.01.18 18:29

    我正在学ASP这个对我有一定的帮助``
  呵呵``
  谢谢哦!

------------------------
用我的心来换你的心!

回    复    

回复


回复主题: 回复在论坛 回复到信箱
回复内容:
附加签名:
上传贴图:
图片要求:长宽建议不超过:650×650。大小:300K 以内,文件后缀名必须为:.gif 或.jpg 或.png
      
版主推荐:
文坛新文:
作者其它文章:

Copyright 2002-2008 版权所有
胡杨林© All rights reserved.
服务支持拓商网