[求助]帮帮我!谢谢!(在线等,问题解决立刻结账)

tandry 2003-10-15 06:07:58
我想通过上传组件把图片存储在数据库中,然后从数据库中取出。运行后没有错误,但是我不知道如何能知道图片存储真正存储在数据库中了,也不知道该如何从数据库中取出该图片。存储该图片的字段
请看关键代码:
<%
on error resume next
dim stu_name
stuname=request("name")
dim fileup , f
set fileup=server.CreateObject("chinaasp.upload")
set f=fileup.files("stupic")
response.Write stuname
if request("submitpic1")<>"" then
dim objrspic , sqlpic
sqlpic="select * from photoinfo where student_name='"&stuname&"'"
set objrspic=server.CreateObject("adodb.recordset")
objrspic.open sqlpic,conn,1,3
response.Write objrspic.recordcount
if objrspic.recordcount<>0 then
response.Write("对不起,您已经有照片了。")
response.Write objrspic("student_name")
objrspic.close
set objrspic=nothing
else
objrspic.addnew
objrspic("student_name")=stuname
objrspic("photo_size")=f.filesize
objrspic("photo_content").appendchunk f.filecontent
objrspic.update
response.Write("恭喜,上传成功!")
objrspic.close
set objrspic=nothing

dim rrs ,ssql , content
set rrs=server.CreateObject("adodb.recordset")
ssql="select * from photoinfo where student_name='"&stuname&"'"
rrs.open ssql,conn,1,3
response.Write rrs.recordcount
content=rrs("photo_content").getchunk(clng(rrs("photo_size")))
response.Write rrs("student_name")
rrs.close
set rrs=nothing
end if
end if
%>
...全文
49 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
1蓝天1 2003-10-15
  • 打赏
  • 举报
回复
up
tandry 2003-10-15
  • 打赏
  • 举报
回复
现在数据库已经可以存储图片了,可是就是取不出来。怎么办? ratnight(ratnight) ( ) 的办法我试了,也是不行。
ghost986212 2003-10-15
  • 打赏
  • 举报
回复
这有人( pp4u(方便面(当天结贴)) )给我这样一个范例,先拿上给你参考。


这个范例共包括三个ASP文件和一个数据库(一个表),全部在同一目录下。

1、tblImage 表结构(ACCESS 2000)

  sn     自动编号 序列号
  content-type 文本   图片类型
  image    OLE 对象 图片数据

2、SimpleImageToData.asp:上传表单及保存图片到数据库的代码部分,主要文件。

<%@ Language=VBScript %>
<% option explicit %>

<%
'从一个完整路径中析出文件名称
function getFileNamefromPath(strPath)
getFileNamefromPath = mid(strPath,instrrev(strPath,"\")+1)
end function

'定义数据库连接字符串
dim cnstr
cnstr = "driver={Microsoft Access Driver (*.mdb)};dbq=" & server.MapPath("./upload.mdb")
%>

<HTML>
<HEAD>
<title>单个图像保存到数据库</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<body>
<p><a href="SimpleImageToData.asp">上传图片</a>
<a href="ShowImageListFromData.asp">显示图片</a><hr></p>

<%
if request.ServerVariables("REQUEST_METHOD") = "POST" then

dim sCome, sGo, binData, strData
dim posB, posE, posSB, posSE
dim binCrlf
dim strPath, strFileName, strContentType

binCrlf = chrb(13)&chrb(10) '定义一个单字节的回车换行符

set sCome = server.CreateObject("adodb.stream")
sCome.Type = 1 '指定返回数据类型 adTypeBinary=1,adTypeText=2
sCome.Mode = 3 '指定打开模式 adModeRead=1,adModeWrite=2,adModeReadWrite=3
sCome.Open
sCome.Write request.BinaryRead(request.TotalBytes)

sCome.Position = 0
binData = sCome.Read

'response.BinaryWrite binData '调试用:显示提交的所有数据
'response.Write "<hr>" '调试用

set sGo = server.CreateObject("adodb.stream")
sGo.Type = 1
sGo.Mode = 3
sGo.Open

posB = 1
posB = instrb(posB,binData,binCrlf)
posE = instrb(posB+1,binData,binCrlf)
'response.Write posB & " | " & posE & "<br/>"

sCome.Position = posB+1
sCome.CopyTo sGo,posE-posB-2
sGo.Position = 0
sGo.Type = 2
sGo.Charset = "gb2312"
strData = sGo.ReadText
sGo.Close

'response.Write strData & "<hr>"

posSB = 1
posSB = instr(posSB,strData,"filename=""") + len("filename=""")
posSE = instr(posSB,strData,"""")

if posSE > posSB then
strPath = mid(strData,posSB,posSE-posSB)
'response.Write "本地路径:" & strPath & "<br/>"
'response.Write "文件名:" & getFileNamefromPath(strPath) & "<br/>"

posB = posE
posE = instrb(posB+1,binData,binCrlf)
'response.Write posB & " | " & posE & "<br/>"

sGo.Type = 1
sGo.Mode = 3
sGo.Open

sCome.Position = posB
sCome.CopyTo sGo,posE-posB-1

sGo.Position = 0
sGo.Type = 2
sGo.Charset = "gb2312"
strData = sGo.ReadText
sGo.Close

strContentType = mid(strData,16) '此处因为固定的,所以省略查找 :-)
'response.Write "图片类型:" & strContentType & "<hr>"

posB = posE+2
posE = instrb(posB+1,binData,binCrlf)
'response.Write posB & " | " & posE & "<br/>"

sGo.Type = 1
sGo.Mode = 3
sGo.Open

sCome.Position = posB+1
sCome.CopyTo sGo,posE-posB-2

sGo.Position = 0
strData = sGo.Read
sGo.Close

'response.Write lenb(strData) & "<br/>"

dim cn, rs, sql
set cn = server.CreateObject("adodb.connection")
cn.Open cnstr
set rs = server.CreateObject("adodb.recordset")
sql = "select * from tblImage"
rs.Open sql,cn,1,3
rs.AddNew
rs.Fields("content-type").Value = strContentType
rs.Fields("image").AppendChunk strData
rs.Update
rs.Close
set rs = nothing
cn.Close
set cn = nothing
response.Write "图片保存成功!" & "<br/>"
else
response.Write "没有上传图片!" & "<br/>"
end if

set sGo = nothing
sCome.Close
set sCome = nothing
else
%>
<form id="frmUpload" name="frmUpload" action="SimpleImageToData.asp" method="post" target="_self" enctype="multipart/form-data">
<INPUT id="filImage" type="file" name="filImage" size="40">
<BR>
<INPUT id="btnUpload" type="submit" value="Upload" name="btnUpload">
</form>
<%
end if
%>
</body>
</HTML>

3、ShowImageListFromData.asp

<%@ Language=VBScript %>
<% option explicit %>

<html>
<head>
<title>显示数据库中已有图片的列表</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<p><a href="SimpleImageToData.asp">上传图片</a>
<a href="ShowImageListFromData.asp">显示图片</a><hr></p>
<table border=0 cellpadding=2 cellspacing=2>
<tr>
<td valign=top>
<%
dim cnstr
cnstr = "driver={Microsoft Access Driver (*.mdb)};dbq=" & server.MapPath("./upload.mdb")

dim cn, sql, rs
set cn = server.CreateObject("adodb.connection")
cn.Open cnstr
sql = "select sn,[content-type],image from tblImage"
set rs = cn.Execute(sql)

response.Write "<table border=1 cellspacing=2 cellpadding=5>"
response.Write "<tr>"
response.Write "<th>序列号</th><th>图片类型</th><th>图片</th>"
response.Write "</tr>"

do until rs.eof
response.Write "<tr>"
response.Write "<td>" & rs("sn") & "</td>"
response.Write "<td>" & rs("content-type") & "</td>"
response.Write "<td><a href='ShowImageListFromData.asp?sn=" & rs("sn") & "'>看图</a></td>"
response.Write "</tr>"
rs.movenext
loop

response.Write "</table>"

cn.Close
set cn = nothing
%>
</td>
<td valign=top>
<%
dim sn
sn = request.QueryString("sn")
if sn = "" then
response.Write "没有指定图片!"
else
response.Write "<img border=1 src=ShowImageFromData.asp?sn=" & sn & ">"
end if
%>
</td>
</tr>
</table>
</body>
</html>

4、ShowImageFromData.asp

<%@ Language=VBScript %>
<% option explicit %>

<%
dim sn
sn = request.QueryString("sn")
if sn = "" then response.End

dim cnstr
cnstr = "driver={Microsoft Access Driver (*.mdb)};dbq=" & server.MapPath("./upload.mdb")

dim cn, sql, rs
set cn = server.CreateObject("adodb.connection")
cn.Open cnstr
sql = "select sn,[content-type],image from tblImage where sn=" & cint(sn)
set rs = cn.Execute(sql)

response.ContentType = rs("content-type")
response.BinaryWrite rs("image")

set rs = nothing
cn.Close
set cn = nothing
%>

purexu 2003-10-15
  • 打赏
  • 举报
回复
ole
tandry 2003-10-15
  • 打赏
  • 举报
回复
数据库中存储图片的字段应该是文本属性呢还是ole对象呢?或者是其他呢?
tandry 2003-10-15
  • 打赏
  • 举报
回复
还是不行呀!
ratnight 2003-10-15
  • 打赏
  • 举报
回复
<img src="showimg.asp">
showimg.asp
_____________________________
set rrs=server.CreateObject("adodb.recordset")
ssql="select * from photoinfo where student_name='"&stuname&"'"
rrs.open ssql,conn,1,3
response.Write rrs.recordcount
content=rrs("photo_content").getchunk(clng(rrs("photo_size")))
response.Write rrs("student_name")
____________________

28,390

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧