欢迎光临
我们一直在努力

ASP文件上传神功 第一重(单个图片上传到数据库)

建站超值云服务器,限时71元/月

第一重:单个图片上传到数据库

  这个问题已经不是什么新鲜问题了,网上也有大把的教程,但大多数是授人以鱼,而不授人以渔,经过辛苦的资料收集,思考,调试,整理,我基本上已经把这个问题从原理上搞清楚了,现在根据我自己的理解,在范例程序的基础上,加以解释,希望能对部分网友(比我还菜的:-))有所帮助。

  请诸位大虾能对其中的不正或不良这处予以指正。

  程序中stream对象的用法上参考了“化境http上传程序 version 2.0”在代码,在此对稻香老农和梁无惧表示衷心的感谢和由衷的敬意。

  我想循序渐进,今天先讲一个简单的,单个图片文件保存到数据库。

这个范例共包括三个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

%>

待续…

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP文件上传神功 第一重(单个图片上传到数据库)
分享到: 更多 (0)

相关推荐

  • 暂无文章