设为首页
加入收藏
站内地图
旧版入口
当前位置:首页 > 站长学院 > 网络编程 > ASP

如何利用ASP显示图片

作者:佚名 出处:网络转载 时间:10-28 点击:

内容载入中...
   一些朋友问到如何显示数据库中的图片?这就是本节所要讨论的主要问题。 

我想可以通过2种方法解决,就ACCESS97数据库而言,一种方法就是在数据库内创建
一个字段保存图片,通过ASP程序来直接显示它;另一个方法就是在数据库内创建一
个普通的文本类型的字段,该字段只保存相应的图片的文件名,将图片放在指定的
目录内,然后通过ASP程序显示它。 

我们先看后者,我认为这个方法简单,数据库操作起来负担轻。 

(1)首先,构造ACCESS97数据库people.mdb,结构如下: 

字段名 类型 长度 描述 
id 自动编号 递增 用户id 
username 文本 20 用户名 
picture 文本 60 图片文件名 
sex 文本 10 性别(男、女) 
age 数字 整型,小数位0 年龄 
email 文本 60 用户邮箱 
info 文本 110 用户描述 

提示:构造合理的数据库结构非常重要,这个例子非常简单。需要指出的是如果
一个字段需要20个字符数就够了,就不要用ACCESS97默认的50,info字段用“备
注型”也可以,但是没有必要,110个足够描述了。这样节省系统资源。尤其是数
据量非常庞大时这个选择的优势就显示出来了。 

(2)我们需要完成这样几个页面,login.htm用于管理员将数据登陆到数据
库内,add.asp用于处理数据的登陆,view.asp显示所有在数据库内的成员
列表,vieweach.asp用于点击单个用户后显示该用户资料。 

额外的说明:本例是将所有的图片放在images目录内。 如果你打算通过这个程
序作个友情连接或交友程序,可以稍加修改,将图片做成指向某个URL的连接即
可。最好的情况是你有一个upload的组件,允许用户上传他们的图片。 

(3)login.htm文件: 

普通的HTML页面,你需要注意的是: <form method="post" action="add.asp">
,即将提交的表单交给add.asp去处理。此外,你还需要为为表单中的每一个
项目命名。本例分别是:username,picture,sex,age,email和info。 
将记录添加到数据库中 Add.asp 

<%@ LANGUAGE="VBSCRIPT" %> 
’告诉服务器处理的脚本类型,可以不要,一般系统默认是vbscript,因为
默认脚本可以通过修改注册表而变化,所以 最好加上。 
<% 
’取得表单输入的数据 
username = Request.form("username") 
picture = Request.form("picture") 
sex = Request.form("sex") 
age = trim(Request.form("age")) 
’去掉输入的age左侧的空格 
email = trim(Request.form("email")) 
’去掉输入的email左侧的空格 
info= trim(Request.form("info")) 
’去掉输入的info左侧的空格 

’下面进行表单项目检测,首先是非空检测 
if username = "" or picture = "" or age = "" or email = "" or 
info = "" then 
Response.Write "<html><body><center><font size=5>请将资料填写完整。</font></center></body></html>" 
Response.end 
end if 

’检测age字段是否是数字 
if not isnumeric(age) then 
Response.Write "<html><body><center><font size=5>年龄应该是数字。</font></center></body></html>" 
Response.end 
end if 


’ 检测email是否输入正确,标准检测方法,今后用它你可以检测email的正确性; 
if inStr(email, "@") = 0 or inStr(email, ".") = 0 then 
Response.Write "<html><body><center><font size=5>Email填写错误。</font></center></body></html>" 
Response.end 
end if 

’与数据库连接 
set dbconnection=Server.CREATEOBJECT("ADODB.CONNECTION") 
DBPath = Server.MapPath("people.mdb") 
dbconnection.Open "driver={Microsoft Access Driver (*.mdb)};dbq="
 & DBPath 

’检测同一个picture是否已经在数据库中存在,即记录是否重复;注意我们这
里只需要picture字段,所以就是select picture,不必select *了,不必要浪
费资源的时候就要节约。还有就是带条件的select语句的写法,有一个单引号,即select picture from people where picture=’1.jpg’。其中pople是数据表名。 


strsql_dup = "select picture from people where picture=’" & picture & "’" 

’创建recordset对象 
Set rs_dup = Server.CreateObject("ADODB.Recordset") 
rs_dup.Open strsql_dup, dbconnection, 3 

’如果有数据重复,显示出错信息; 
if not rs_dup.eof then 
Response.Write "<html><body><center><font size=5>该记录在数据库中已有。</font></center></body></html>" 
rs_dup.close 
set rs_dup = nothing 
response.end 

’否则进行添加操作; 
else 
Set rs_add = Server.CreateObject("ADODB.Recordset") 
rs_add.Open "people", dbconnection, adOpenDynamic, 3,AdCmdTable 

’加入数据记录 
rs_add.AddNew 
rs_add("username") = username 
rs_add("picture") = picture 
rs_add("sex") = sex 
rs_add("age") = age 
rs_add("email") = email 
rs_add("info") = info 
rs_add.Update 

end if 

’ 记录加入数据库后,导向view.asp查看数据; 
’ 通过view.asp查看所有的记录连接,查看具体的某一条记录通过viewsach.asp控制; 
response.redirect "view.asp" 

’关闭数据库连接并清除内存; 

rs_add.close 
set rs_add = nothing 
dbconnection.close 
set dbconnection = nothing 
%> 

添加记录后显示数据库内的人员列表,显示的 用户名应该是超级连接形式,
同时将用户id传递给下一个页面,以便于显示单个用户资料。传递的方法是
通过环境变量QueryString进行的。 

view.asp 

<%@ LANGUAGE="VBSCRIPT" %> 

<% 
set connection=Server.CREATEOBJECT("ADODB.CONNECTION") 
DBPath = Server.MapPath("people.mdb") 
connection.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath 

’创建sql语句,这里只需要用户id和username,需要id是因为要传递到下一个页
面中去。 

strsql = "select id,username from people" 
’创建recordset对象 
Set rs = Server.CreateObject("ADODB.Recordset") 
rs.Open strsql, connection, 3 
%> 
<html> 
<head> 
<title>显示所有人员名单</title> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
</head> 
<body bgcolor="<div align="center"> 
<table width="30%" border="1" cellspacing="0" cellpadding="0" bordercolor="<tr> 
<td>如下是已经登陆的人员名单,点击人名可看详细资料:</td> 
</tr> 
<%if rs.eof or rs.bof then%> 
<tr><td>目前暂时没有记录。</td></tr> 
<% 
else 
RS.movefirst ’移动到下一个记录 
do while not rs.EOF ’使用do while...loop循环显示所有记录 
%> 
<tr><td> 
<a href="’menubar=no,location=no,toolbars=no,top=0,height=250,
width=300’)"><%=rs("username")%> </a> 
</td></tr> 
<% 
rs.movenext 
loop 
end if 
%> 
<% 
rs.close 
set rs = nothing 
connection.close 
set connection = nothing 
%> 
</table> 

</div> 
</body> 
</html> 

注意:上面天蓝色部分是重点,注意表示成超级连接的写法,还有我使用了弹
出式窗口来完成,对应的连接大概是这样:<a href="vieweach.asp?id=1">
用户一</a>,紧接着问号后面的就是环境变量名id,等号后面的就是变量的值
,如果需要传递多个变量到后续页面中,变量间用“&”连接即可。例如:<a href="vieweach.asp?id=1&name=james">等。在下一个页面中就可以用request.querystring("变量名")取得传递过来的变量值。 

在上面的代码中,<%=rs("id")%>和<%=rs("username")%>可以得到对应的
值用户id和用户名。 

获得上一页面传递来的用户id,并显示该id号的用户资料。 

vieweach.asp 

<%@ LANGUAGE="VBSCRIPT" %> 

<% 
’ 取得通过环境变量querystring传递来的参数id. 
id = request.querystring("id") 

set connection=Server.CREATEOBJECT("ADODB.CONNECTION") 
DBPath = Server.MapPath("people.mdb") 
connection.Open "driver={Microsoft Access Driver (*.mdb)};
dbq=" & DBPath 

strsql = "select * from people where id=" & id 
’创建recordset对象 
Set rs = Server.CreateObject("ADODB.Recordset") 
rs.Open strsql, connection, 3 
%> 

<html> 
<head> 
<title><%=rs("username")%>的资料</title> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
</head> 

<body bgcolor="<div align="center"> 
<table width="47%" border="1" bordercolor="<tr> 
<td rowspan="4" width="30%"><img src="images/<%=rs("picture")%>"
 width="120" height="155" alt="<%=rs("username")%>的酷照"></td> 
<td width="70%"><%=rs("username")%></td> 
<td width="70%"><%=rs("sex")%></td> 
</tr> 
<tr> 
<td width="140%" colspan="2">年龄:<%=rs("age")%></td> 
</tr> 
<tr> 
<td width="140%" colspan="2"><a href="mailto:<%=rs("email")%>">
<%=rs("email")%></a></td> 
</tr> 
<tr> 
<td width="140%" height="21" valign="top" colspan="2"><%=
rs("info")%></td> 
</tr> 
</table> 
<p><a href="javascript:window.close();">关闭</a></p> 
</div> 
</body> 
</html> 
<% 
rs.close 
set rs = nothing 
connection.close 
set connection = nothing 
%> 

注意:所有的图片豆放在images目录内。
收藏本文:
】【打印页面】【推荐给朋友】【关闭窗口

站长学院

推荐信息