电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> 数据库
用ASP实现多图片上传到指定的目录并存到数据库
作者:网友供稿 点击:8
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 
关于图片上传的例子在网上有很多文章和原代码。但是每次上论坛浏览帖子的时候都是看到很多网友对图片上传感到头疼和麻烦。其实这个问题也是曾经让我感到头疼。也看过了不少的文章和代码。现在我写的这篇文章是把我比较喜欢的一篇代码和动网里的高手对这篇代码优化后再加上我增加一些代码结合出来的!呵呵,其实这篇文章是沾了写这篇代码的人(稻香居士)和动网里那些高手的光。:)
好了,不说废话了。开始来搭建所需要的环境和数据结构!
先新建一个名字叫photo的文件夹。(我在这里就是把图片上传到这个文件夹里的。)建立一个名字叫database的数据库。再接着建立一个名字叫sfile的表。表里设计四个字段分别是id(int),image(varchar),image1(varchar),image2(varchar)。我这里用的数据库是sql。
相关的文件
register.asp
<html>
<head>
<title>文件</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<form name="form1" method="post" action="save.asp">
<table width="50%" border="1" cellspacing="0" cellpadding="0" align="center">
<tr>
<td width="20%"><a href="#" onclick="javascript:window.open(/reg_upload.asp,null, left=40%,top=40%,height=250,width=470,status=yes,toolbar=no,menubar=no,resizable=yes,copyhistory=yes,scrollbars=yes,location=no,status=no,titlebar=no)">图片上传</a></td>
<td width="80%">
<input type="text" name="myface">

<input type="text" name="myface1">

<input type="text" name="myface2">
(此处用户不必填图片上传后自动生成) </td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="提交">
<input type="reset" name="submit2" value="重设">
</td>
</tr>
</table>
</form>
</body>
</html>

reg_upload.asp
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body leftmargin="0" topmargin="0" >
  <br>
<br>
<br>
<table width="90%" border="0" align="center" bgcolor="#000000" height="152" cellspacing="1">
<tr>
<td height="34" align="center" bgcolor="#ffffff"><font color="#ffff33"><b><font size="4" color="#000000">选择图片</font></b></font></td>
</tr>
<tr>
<td bgcolor="#ffffff">
<form name="form" method="post" action="upfile.asp" enctype="multipart/form-data" >
<input type="hidden" name="filepath" value="photo">
<input type="hidden" name="act" value="upload">
<input type="file" name="file1" size="30"><br>
<input type="file" name="file2" size="30"><br>
<input type="file" name="file3" size="30">

<input type="submit" name="submit" value="粘 贴" class="tl">
</form>
</td>
</tr>
</table>
</body>
</html>

upfile.asp
<!--#include file="upload.inc"-->
<html>
<head>
<title>文件上传</title>
</head>
<body>
<%
dim arr(3)
dim upload,file,formname,formpath,icount,filename,fileext,i
set upload=new upload_5xsoft 建立上传对象


formpath=upload.form("filepath")
在目录后加(/)
if right(formpath,1)<>"/" then formpath=formpath&"/"
icount=0
i=0
for each formname in upload.file 列出所有上传了的文件
set file=upload.file(formname) 生成一个文件对象

if file.filesize<0 then
response.write "<font size=2>请先选择你要上传的图片 [ <a href=# onclick=history.go(-1)>重新上传</a> ]</font>"
response.end
end if

if file.filesize>100000 then
response.write "<font size=2>图片大小超过了限制 [ <a href=# onclick=history.go(-1)>重新上传</a> ]</font>"
response.end
end if
if fileext<>".gif" and fileext<>".jpg" then
response.write "<font size=2>文件格式不对 [ <a href=# onclick=history.go(-1)>重新上传</a> ]</font>"
response.end
end if

fileext=lcase(right(file.filename,4))
filename=formpath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&file.filename
arr(i)=filename
i=i+1

if file.filesize>0 then 如果 filesize > 0 说明有文件数据
file.saveas server.mappath(filename) 保存文件
response.write file.filepath&file.filename&" ("&file.filesize&") => "&formpath&file.filename&" 成功!<br>"

icount=icount+1
end if
set file=nothing
next
if instr(arr(0),".")=0 and instr(arr(1),".")=0 and instr(arr(2),".")=0 then
response.write "<font size=2>请先选择你要上传的图片 [ <a href=# onclick=history.go(-1)>重新上传</a> ]</font>"
response.end
end if
if instr(arr(0),".")<>0 then
response.write "<script>opener.document.forms[0].myface.value=" & arr(0) & "</script>"
else
response.write "<script>opener.document.forms[0].myface.value=</script>"
end if
if instr(arr(1),".")<>0 then
response.write "<script>opener.document.forms[0].myface1.value=" & arr(1) & "</script>"
else
response.write "<script>opener.document.forms[0].myface1.value=</script>"
end if
if instr(arr(2),".")<>0 then
response.write "<script>opener.document.forms[0].myface2.value=" & arr(2) & "</script>"
else
response.write "<script>opener.document.forms[0].myface2.value= </script>"
end if
set upload=nothing 删除此对象

session("upface")="done"

htmend icount&" 个文件上传结束!"

sub htmend(msg)
set upload=nothing
response.write "<html><head><meta http-equiv=refresh content=3 url=""javascript:window.close();""></head><body><center><br><br>文件上传成功<br>谢谢你的支持!<br>本窗口三秒后自动关闭</center></body></html>"
response.end
end sub
%>
</body>
</html>
upload.inc
<script runat=server language=vbscript>

dim upfile_5xsoft_stream

class upload_5xsoft

dim form,file,version

private sub class_initialize
dim istart,ifilenamestart,ifilenameend,iend,vbenter,iformstart,iformend,thefile
dim strdiv,mformname,mformvalue,mfilename,mfilesize,mfilepath,idivlen,mstr
version=""
if request.totalbytes<1 then exit sub
set form=createobject("scripting.dictionary")
set file=createobject("scripting.dictionary")
set upfile_5xsoft_stream=createobject("adodb.stream")
upfile_5xsoft_stream.mode=3
upfile_5xsoft_stream.type=1
upfile_5xsoft_stream.open
upfile_5xsoft_stream.write request.binaryread(request.totalbytes)

vbenter=chr(13)&chr(10)
idivlen=instring(1,vbenter)+1
strdiv=substring(1,idivlen)
iformstart=idivlen
iformend=instring(iformstart,strdiv)-1
while iformstart < iformend
istart=instring(iformstart,"name=""")
iend=instring(istart+6,"""")
mformname=substring(istart+6,iend-istart-6)
ifilenamestart=instring(iend+1,"filename=""")
if ifilenamestart>0 and ifilenamestart<iformend then
ifilenameend=instring(ifilenamestart+10,"""")
mfilename=substring(ifilenamestart+10,ifilenameend-ifilenamestart-10)
istart=instring(ifilenameend+1,vbenter&vbenter)
iend=instring(istart+4,vbenter&strdiv)
if iend>istart then
mfilesize=iend-istart-4
else
mfilesize=0
end if
set thefile=new fileinfo
thefile.filename=getfilename(mfilename)
thefile.filepath=getfilepath(mfilename)
thefile.filesize=mfilesize
thefile.filestart=istart+4
thefile.formname=formname
file.add mformname,thefile
else
istart=instring(iend+1,vbenter&vbenter)
iend=instring(istart+4,vbenter&strdiv)

if iend>istart then
mformvalue=substring(istart+4,iend-istart-4)
else
mformvalue=""
end if
form.add mformname,mformvalue
end if

iformstart=iformend+idivlen
iformend=instring(iformstart,strdiv)-1
wend
end sub

private function substring(thestart,thelen)
dim i,c,stemp
upfile_5xsoft_stream.position=thestart-1
stemp=""
for i=1 to thelen
if upfile_5xsoft_stream.eos then exit for
c=ascb(upfile_5xsoft_stream.read(1))
if c > 127 then
if upfile_5xsoft_stream.eos then exit for
stemp=stemp&chr(ascw(chrb(ascb(upfile_5xsoft_stream.read(1)))&chrb(c)))
i=i+1
else
stemp=stemp&chr(c)
end if
next
substring=stemp
end function

private function instring(thestart,varstr)
dim i,j,bt,thelen,str
instring=0
str=tobyte(varstr)
thelen=lenb(str)
for i=thestart to upfile_5xsoft_stream.size-thelen
if i>upfile_5xsoft_stream.size then exit function
upfile_5xsoft_stream.position=i-1
if ascb(upfile_5xsoft_stream.read(1))=ascb(midb(str,1)) then
instring=i
for j=2 to thelen
if upfile_5xsoft_stream.eos then
instring=0
exit for
end if
if ascb(upfile_5xsoft_stream.read(1))<>ascb(midb(str,j,1)) then
instring=0
exit for
end if
next
if instring<>0 then exit function
end if
next
end function

private sub class_terminate
form.removeall
file.removeall
set form=nothing
set file=nothing
upfile_5xsoft_stream.close
set upfile_5xsoft_stream=nothing
end sub


private function getfilepath(fullpath)
if fullpath <> "" then
getfilepath = left(fullpath,instrrev(fullpath, "\"))
else
getfilepath = ""
end if
end function

private function getfilename(fullpath)
if fullpath <> "" then
getfilename = mid(fullpath,instrrev(fullpath, "\")+1)
else
getfilename = ""
end if
end function

private function tobyte(str)
dim i,icode,c,ilow,ihigh
tobyte=""
for i=1 to len(str)
c=mid(str,i,1)
icode =asc(c)
if icode<0 then icode = icode + 65535
if icode>255 then
ilow = left(hex(asc(c)),2)
ihigh =right(hex(asc(c)),2)
tobyte = tobyte & chrb("&h"&ilow) & chrb("&h"&ihigh)
else
tobyte = tobyte & chrb(ascb(c))
end if
next
end function
end class


class fileinfo
dim formname,filename,filepath,filesize,filestart
private sub class_initialize
filename = ""
filepath = ""
filesize = 0
filestart= 0
formname = ""
end sub

public function saveas(fullpath)
dim dr,errorchar,i
saveas=1
if trim(fullpath)="" or filesize=0 or filestart=0 or filename="" then exit function
if filestart=0 or right(fullpath,1)="/" then exit function
set dr=createobject("adodb.stream")
dr.mode=3
dr.type=1
dr.open
upfile_5xsoft_stream.position=filestart-1
upfile_5xsoft_stream.copyto dr,filesize
dr.savetofile fullpath,2
dr.close
set dr=nothing
saveas=0
end function
end class
</script>
save.asp
<% dim dsn,conn
dsn="driver={sql server};server=127.0.0.1;uid=sa;pwd=;database=database"
set conn=server.createobject("adodb.connection")
conn.open dsn
%>
<% dim file,file1,file2
file=trim(request.form("myface"))
file1=trim(request.form("myface1"))
file2=trim(request.form("myface2"))
set rs=server.createobject("adodb.recordset")
sql="insert into sfile(image,image1,image2) values("& file &","& file1&","& file2&")"
conn.execute(sql)
response.redirect "ok.asp"
%>
呵呵,代码多了点。复制以上的代码到你的机器上运行是不是你想要的结果!至于显示图片相信大家都明白了吧!我就不必多说了。值得一提的是此代码不仅可以用于图片上传而是任何格式的文件都可以哦,至于文件大小也可以由你自己来定义哦!还有的就是我只定义了上传三个要是要上传更多的话稍为改下代码就可以了。仔细看代码就知道了是不是觉得很方便和实用。这个只不过是在我解决这个问题的时候用的一种方法而已。你要是有更好的方法来实现也可以来email告诉我!互相学习共同进步。第一次写文章可能表达方面有语法问题。要是有什么问题也可以来email告诉我!我的邮箱:hushuowang@163.net,qq:11313112。时间不早了,我要睡觉了!呼噜呼噜zzzzzz....
(特别感谢hooke等高手一直以来的技术支持)小汪仔于2002/5/26晚


文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·数据库开发个人总结(ADO.NET小结)-.NET教程,数据库应用
·怎么由DataSet将数据导入Excel?-.NET教程,数据库应用
·动态创建SQL Server数据库、表、存储过程-ASP教程,数据库相关
·Win32环境下动态链接库(DLL)编程原理-.NET教程,数据库应用
·封装的ADO.NET对数据库操作经典类-.NET教程,数据库应用
·在DataGridView中获得DataGridViewCheckBoxColumn的状态-ASP教程,数据库相关
·DataGrid使用心得(附大量代码)-ASP教程,数据库相关
·用代码创建DataGrid的多链接及checkbox事件响应-.NET教程,数据库应用
·ADO.NET 的最佳实践技巧-.NET教程,数据库应用
·转载: 用纯ASP代码实现图片上传并存入数据库中

最新文章
·根据数据表中数据,生成Powerpoint幻灯片-ASP教程,数据库相关
·DataGrid中的按钮反选事件与NamingContainer(命名容器)-downmoon-ASP教程,数据库相关
·使用用VB处理MYSQL数据库中二进制数据问题-.NET教程,VB.Net语言
·关于DataGridView中如何接收处于编辑状态下的当前信息-ASP教程,数据库相关
·在DataGridView中获得DataGridViewCheckBoxColumn的状态-ASP教程,数据库相关
·.net下访问Access数据库需要注意的问题-.NET教程,Asp.Net开发
·ActiveMQ4.1+Spring2.0的POJO JMS方案(上)-.NET教程,数据库应用
·ASP.NET 2.0中直接将Access数据库导入到Excel文件中-.NET教程,Asp.Net开发
·NET(C#)连接各类数据库-集锦-.NET教程,C#语言
·ASP.NET2.0连接SQL Server数据库详解-.NET教程,Asp.Net开发




版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号