同时上传图片以及图片的文字说明
发现有的朋友对于同时上传图片以及图片的文字说明还是有点不了解今天我抽了点小时间
弄了一个小小的东西
希望能对大家有点作用哈~~!!(^#@$%^ (^#@$%^
在这里,我们先建好一个数据库:
数据库名为test.mdb
表名为:test
里面4个字段:id(自动编号),111,222,333
数据库建立完成
再新建一个名为images的空文件夹
OK了,我们开始做一个上传页面:upfile.htm
代码如下:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<script language="javascript">
function aa() {
document.form1.submit();
document.form2.submit();
document.form3.submit();
}
function bb(){
document.form1.reset();
document.form2.reset();
document.form3.reset();
}
</script>
<form name="form1" method="post" action="a.asp">
<input type="text" name="text1" />
</form>
<form name="form2" method="post" action="b.asp">
<input type="text" name="text2" />
</form>
<form method="POST" name="form3" enctype="multipart/form-data" action="SavePic.asp">
<p>
<input type="file" name="file" size="20">
</p>
</form>
<input type="button" name="btnUp" onclick="return aa();" value="提交">
<input type="reset" value="reset" onclick="return bb();" />
</body>
</html>
我们再建三个上传页面,分别是上传图片说明的a.asp,图片连接b.asp以及图片SavePic.asp.其实a.asp和b.asp可以放在同一个form中,我这么分开写只是为了给大家说明一个方法....
代码分别如下:
a.asp
<%Dim connstr,conn,text1
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("test.mdb")
Set conn=Server.CreateObject("ADODB.Connection")
conn.open connstr
text1=trim(Request.Form("text1"))
Set rs=Server.CreateObject("ADODB.RecordSet")
rs.open "select * from test",conn,3,3
rs.addnew
rs("111")=text1
rs.update
rs.close
Set rs=nothing
conn.close
Set conn=nothing
%>
b.asp
<%Dim connstr,conn,text2
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("test.mdb")
Set conn=Server.CreateObject("ADODB.Connection")
conn.open connstr
text2=trim(Request.Form("text2"))
Set rs=Server.CreateObject("ADODB.RecordSet")
rs.open "select * from test",conn,3,3
rs.Movelast
rs("222")=text2
rs.update
rs.close
Set rs=nothing
conn.close
Set conn=nothing
%>
SavePic.asp
<%
dim UpFileSize,contentlen
UpFileSize = 500 '文件大小限制,单位:k
contentlen=request.totalbytes '接收表单信息
'检测文件大小
if contentlen>UpFileSize*1024 then
Response.write"<script>alert('上传的文件过大');window.location.href='upfile.htm';</script>"
Response.end
else
dim content '读取上传文件信息
content=request.binaryread(request.totalbytes)
'二进制相互转换函数
Function getByteString(StringStr)
For i=1 to Len(StringStr)
char=Mid(StringStr,i,1)
getByteString=getByteString&chrB(AscB(char))
Next
End Function
Function getString(StringBin)
getString =""
For intCount = 1 to LenB(StringBin)
getString = getString & chr(AscB(MidB(StringBin,intCount,1)))
Next
End Function
'处理数据
dim upbeg,upend,lineone,linetwo,linethree,line1,line2,line3
upbeg=1
upend=instrb(upbeg,content,getbytestring(chr(10)))
lineone=midb(content,upbeg,upend-upbeg)
upbeg=upend+1
line1=lenb(lineone)
upend=instrb(upbeg,content,getbytestring(chr(10)))
linetwo=midb(content,upbeg,upend-upbeg)
upbeg=upend+1
line2=lenb(linetwo)
upend=instrb(upbeg,content,getbytestring(chr(13)))
linethree=midb(content,upbeg,upend-upbeg)
line3=lenb(linethree)
'获得文件名
dim pp,checknametemp,checklen,checkname,filename
pp=instrb(1,linetwo,getbytestring(chr(46)))
checknametemp=rightb(linetwo,line2-pp+1)
checklen=instrb(1,checknametemp,getbytestring(chr(34)))
checkname=getstring(leftb(checknametemp,checklen-1))
'检测文件后缀
if checkname<>"Content-Disposition: form-data; name=" then
if checkname=".gif" or checkname=".jpg" or checkname=".jpeg" or checkname=".bmp" then
filename=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&checkname '定义文件名
'上传文件
dim alllen,upstream,upstreamend,file
alllen=line1+line2+line3+6
set upstream=server.createobject("adodb.stream")
set upstreamend=server.createobject("adodb.stream")
upstream.type=1
upstreamend.type=1
upstream.open
upstreamend.open
upstream.write content
upstream.position=alllen
file=upstream.read(clng(contentlen-alllen-line1-5))
upstreamend.write file
upstreamend.savetofile(Server.MapPath("images/" & filename))
upstream.close
upstreamend.close
set upstream=nothing
set upstreamend=nothing
'//写入到数据库
Set conn = Server.CreateObject("ADODB.Connection")
conn.open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("test.mdb")
set rs=server.CreateObject("adodb.recordset")
rs.open "Select * from test",conn,3,3
rs.movelast
rs("333")=filename
rs.update
set rs=nothing
conn.close
set conn=nothing
Response.write"文件上传成功,文件文件路径:images/" & filename & "<br><br>"
else
Response.write"<script>alert('不允许上传" & checkname & "格式的文件!');window.location.href='Book_List';</script>"
Response.end
end if
else
Response.write"<script>alert('你没有选择文件!');window.location.href='upfile.htm';</script>"
end if
end if
%>
还没有看明白的话,就下载我的附件吧!!!!!
不好意思,贵是贵了点,但是绝对划算啊!!!! 非常感谢~~~我会了~~~
但有没有办法给传的文件~~指定一个名字~~~~
就是不管你上传文件的名字叫什么~~~上传到服务器后就被重命名为指定的~ 你想给图片指定一个名字是可以的
你只要把上面的那些定义filename的东西删除掉
并制定filename="filename"就行了(^#@^ 加入这论坛,,真是获益非浅`啊~~(^#@$%^ 还是有问题~~~
我把"SavePic.asp"中的
filename=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&checkname '定义文件名
改成
filename="filename"&checkname '定义文件名
之后是定义了第一张图片的名字为"filename.***"但是当我上传第二张的时候就提示出错~~
下面是错误信息
技术信息(用于支持人员)
错误类型:
ADODB.Stream (0x800A0BBC)
写入文件失败。
/SavePic.asp, 第 72 行
浏览器类型:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.2)
网页:
POST 80088 ??? /SavePic.asp
POST Data:
错误 '80020009'
发生意外。
/iisHelp/common/500-100.asp,行 219
我想是检测到服务器上已经有一张名为"filename.***"的图片,,不允许覆盖~~的错误 ....
我还真的没有去碰到这个问题!!!!
等我搞明白了回来再告诉你#@jcwcn^@@# 下了之后我发现,其实不用下载,因为你上面说的和下的一样啊``#@#$#$ ...
我主要是怕有的朋友不知道具体怎么弄啊!!!
所以放上去了
呵呵#bbs.jcwcn.com@ (^#@^ 希望可以解決不能寫入文字的問題 1.請問要寫入type="checkbox"要怎麼寫?
2.請問上傳的圖片檔名怎麼改原檔名?
回复 9# alam4207 的帖子
经检测可以输入文字的啊~#kt..jcwcn.com@回复 10# alam4207 的帖子
回答你第二个问题...第一个问题你没有怎么描述清楚
第二个问题
你要是想用原文件名的话呢
只需要把
filename=year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&checkname '定义文件名
改成filename=file.FileName
但是我绝对不推荐你用这样的方法
以后你上传东西多了以后呢
难免会碰到文件名相同的文件
这样的话就会把以前的文件给覆盖掉了
#kt..jcwcn.com@ #kt..jcwcn.com@ 你好:
1.請問怎麼把rs.open "Select * from bala",conn,3,3
修改成sql="select * from bala where id=" & id1
2.請問怎麼把rs("pic")=filename
修改成sql=sql & "pic='" & pic & "'"
因為寫法好像不一樣 我不會改 我要做修改資料庫資料 我是想把SavePic.asp改成修改的模式
id1=request.QueryString("id1").Item
sql="Update bala SET "
sql=sql & ",pic='" & filename & "'"
sql=sql & " where id=" & id1
這四段怎麼加進去 相当复杂啊!!!!!!!!! 这个论坛真好啊 学习的东西可真多
国内优秀的IIS虚拟主机监控系统-德州畅想中国IIS虚拟主机监控系统
*** 作者被禁止或删除 内容自动屏蔽 ***页:
[1]
