这个上传图片到数据库的模式,是弹出图片添加对话框,request只用于提交图片对象,不提交其他参数,省去分析提交的其他数据项,其他数据项通过session提交。
图片单独一个表,由id和内容组成,id自动编号。图片和对象关系做一个表,关键字自动编号,一个记录存储图片id、对象id、和图片类型。对象id对应其他的表中的数据,图片类型可作扩展需要,比如根据不同要求显示不同的样式。
首先程序提交图片选择页面(img_insert.asp),通过imagetype变量,转入不同的对象提交页面(如img_bd.asp),提交完成后返回的页面、对象id及图片类型等参数,调用插入函数(uploadimg),
将图片插入image表后,再将图片id,对象id,图片类型插入relation_image表,然后转入返回的页面(img_back.asp),返回图片id等参数。返回页面在规定时间内自动关闭,回到完成后返回的页面。
<a href="img_insert.asp?imagetype=2&mainid=<%=对象id%>" target="_blank">添加图片</a>
img_insert.asp文件代码,可以生成上传前的预览。
功能还可以根据具体情况进行扩展,如添加检查文件类型的判断程序
<html xmlns="http://www.w3.org/1999/xhtml">
<!--#include file="../connect/connect.asp"-->
<head>
<script type="text/javascript">
function showimg(){ //在网页“showpic”控件中生成图片预览
if (document.picForm.picture.value!=""){
document.all("showpic").src=document.picForm.picture.value;
}
}
function uploadImg(imgtype) {
if(document.picForm.picture.value==""){
alert("请点击“浏览”选择文件");
return; }
if(document.all("showpic").fileSize>(300*1024)){ //检查文件大小是否超限
alert("选择的文件超过300K,请重新选择");
return;
}
showTipInfo("正在上传图片");
if (imgtype==1) {
document.picForm.action = "img_bd.asp";
}
else if (imgtype==2){
document.picForm.action = "其他页面";
}
else {
document.picForm.action = "../public/error.asp?strerror=有错误";
}
//document.picForm.target = "iframe_upload";
document.picForm.submit();
//}
}
function showTipInfo(text, tipBox) {
var body = document.body;
if (!tipBox) {
var tipBox = document.createElement("div");
body.appendChild(tipBox);
}
tipBox.innerHTML = text;
tipBox.id = "tipBoxDiv";
tipBox.style.color = "#333";
tipBox.style.border = "2px solid #cecece";
tipBox.style.background = "#ffffe1";
tipBox.style.padding = "10px";
tipBox.style.display = "block";
tipBox.style.zIndex = "1";
tipBox.style.position = "absolute";
var x = (body.offsetWidth - tipBox.offsetWidth)/2;
var y = Math.ceil((document.documentElement.clientHeight - tipBox.offsetHeight)/2) + document.documentElement.scrollTop;
tipBox.style.left = x + "px";
tipBox.style.top = y + "px";
}
function onCancel(){cancel();}
// -->
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<link rel="stylesheet" type="text/css" href="../style/popwindows.css" />
<title>添加/修改图片</title>
<%dim imgtype
if request("imagetype")<>"" then
imgtype=request("imagetype")
session("curmainid")=request("mainid")
else
imgtype=0
session("curmainid")=""
end if
%>
</head>
<body οnlοad="window.resizeTo(400,300)">
<div align="left">
<form enctype="multipart/form-data" method="post" name="picForm">
请选择要上传的文件: <br/>
<div id="imgBox_1">
<input name="picture" type="file" class="text" style="width: 90%;" title="“浏览”选择本地图片" οnchange="showimg();" /><br>
<div>图片文件最大为300k<br />
</div>
<div id="artItem-button">
<input type="button" name="ok" value="确 定" class="button-submit" id="imgBut_1" οnclick="uploadImg(<%=imgtype%>);" />
<input type="button" name="cancel" value="取 消" class="button2" οnclick="return onCancel();" />
</div>
</div></form>
<img width="250" height="150" name="showpic"/>
</div>
</body>
</html>
img_bd.asp文件代码
<!--#include file="../connect/connect.asp"-->数据库连接文件
<!--#include file="img_upimg.asp"-->上传图片函数
<%
call uploadimg("pmdj2.asp",session("curmainid"),1)
%>
<!--#include file="../connect/closecon.asp"-->
img_upimg.asp文件代码
<%
'web 插入图片后回到的页面,mainid 图片对应对象的id ,imgtype 图片类型
sub uploadimg(web,mainid,imgtype)
dim FormSize
dim FormData
dim bnCRLF
dim Divider
dim DataStart
dim DataEnd
dim strlen
dim img
dim imageid
FormSize=Request.TotalBytes
FormData=Request.BinaryRead(FormSize)
bnCRLF=chrB(13) & chrB(10)
Divider=LEFTB(FormData,INSTRB(FormData,bnCRLF)-1)
DataStart=INSTRB(FormData,bnCRLF & bnCRLF)+4
DataEnd=INSTRB(DataStart+1,FormData,divider)-DataStart
img=MIDB(FormData,DataStart,DataEnd)
strlen=len(img)
if strlen<155000 then
dim resbuff
set resbuff=server.CreateObject("adodb.recordset")
resbuff.open "SELECT * FROM image where image_id is null",conn,1,2
if err.number <> 0 then
response.write "数据库操作失败:"&err.description
response.redirect"error.htm"
else
resbuff.addnew
resbuff("p_i_content").appendchunk img
imageid=resbuff("p_i_id")
resbuff.update '
end if
resbuff.close
set resbuff=nothing
conn.execute "insert into relation_image(r_i_imageid,r_i_objectid,r_i_type) values("& imageid &",'"& mainid &"',"& imgtype &")"
response.Redirect("img_back.asp?imageid="&imageid &"&imgtype="& imgtype &"&web=" & web)
else
Response.write("图片不能大于300k!")
Response.write("<A href='"& web &"' >返回</a>")
end if
end sub
%>
img_back.asp文件内容,把图片id和图片类型传回
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>图片添加完成</title>
<script language="JavaScript">
function closeit(id)
{ //setTimeout("self.close()",10000) //毫秒
// document.imagefrom.action = "pmdj2.asp";
//document.imagefrom
document.imagefrom.submit();
setTimeout("self.close()",500);
//window.close
}
</script>
</head>
<body οnlοad="closeit()" >
<form name="imagefrom" action = "<%=request("web")%>" target="mainFrame" >
<input name="imageid" type="hidden" value="<%=request("imageid")%>">
<input name="imagetype" type="hidden" value="<%=request("imagetype")%>">
</form>
</body>
</html>