1、导入smartupload.jar包 ,添加uploadIMG.jsp,upfileIMG.jsp.
2、需要在项目下面建立一个保存文件的文件夹pic或者upload
3、在调用的地方调用子框架uploadIMG.jsp上传图片的文件frmName表示的是表单名称ImgName保存图片文件路径的文本输入框filepath表示的是图片文件保存的位置
<iframe src="uploadIMG.jsp?frmName=loginform&ImgName=imgs&filepath=pic" name="uploadpic" width=100% marginwidth="0" height=30 marginheight="0" scrolling=no frameborder=0>
</iframe>
4、uploadIMG.jsp里面就是上传文件表单需要注意上传的格式为 enctype="multipart/form-data"
5、upfileIMG.jsp里面注意获取正确的路径,用时间格式来生成新的图片名称
7、数据用javascript返回
<script>
parent.document.<%=request.getParameter("frmName")%>.<%=request.getParameter("ImgName")%>.value="<%=imgTxt%>" ;
//将图片的保存路径返回给父窗口里面表单所对应的图片的文本输入框
parent.document.getElementById("showimg").innerHTML="<img src=<%=basePath+imgTxt%> width=100 height=100>"
//将图片显示到父窗口在id=showimg的层里面
</script>
示例代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- saved from url=(0055)http://192.168.6.163:8080/sh/manage/links/links_add.jsp --> <HTML><HEAD><TITLE>友情链接管理</TITLE> <META content="text/html; charset=utf-8" http-equiv=Content-Type><LINK rel=stylesheet type=text/css href="../css/style1.css"> <SCRIPT language=JavaScript> function checknull() { if (document.all("sitename").value == ""){ document.getElementById("msg").innerHTML = "友情链接名不能为空!"; return false; } return true; } function addnew(){ if(checknull()){ document.forms["myForm"].submit(); } } </SCRIPT> <META name=GENERATOR content="MSHTML 8.00.7601.18934"></HEAD> <BODY class=main_body> <FORM method=post name=myForm action=LinkServlet> <TABLE border=0 cellSpacing=5 cellPadding=5 width="102%"> <TBODY> <TR> <TD width="72%"> <TABLE border=0 cellSpacing=0 cellPadding=0 width="96%" align=center> <TBODY> <TR> <TD height=20> <TABLE border=0 cellSpacing=0 cellPadding=0 width="100%"> <TBODY> <TR> <TD background=../images/wz_bg.gif width="5%"><IMG src="../images/wz_01.gif" width=24 height=24></TD> <TD background=../images/wz_bg.gif width="18%"> <SPAN class=text_write>友情链接新增</SPAN></TD> <TD width="5%"><IMG src="../images/wz_02.gif" width=33 height=24></TD> <TD width="50%"></TD> <TD align=middle></TD> <TD width="6%" align=middle><A href="javascript:history.go(-1);"><IMG title=返回 border=0 src="../images/icon_back.gif" width=16 height=12></A></TD></TR></TBODY></TABLE></TD></TR> <TR> <TD height=20> </TD></TR></TBODY></TABLE> <TABLE border=0 cellSpacing=1 cellPadding=1 width="96%" bgColor=#9f9d9f align=center> <TBODY> <TR class=td1> <TD width="15%" align=right>友情链接名:</TD> <TD width="85%"><INPUT id=title maxLength=50 size=50 name=title> <FONT color=#ff0000>* <DIV id=msg></DIV></FONT></TD></TR> <TR class=td1> <TD width="15%" align=right>图片路径:</TD> <TD width="85%"> <INPUT id=imgpath maxLength=50 size=50 type="hidden" name=imgpath> <div id="showimg" ></div> <iframe src="uploadIMG.jsp?frmName=myForm&ImgName=imgpath&filepath=pic" name="uploadpic" width=100% marginwidth="0" height=30 marginheight="0" scrolling=no frameborder=0> </iframe> </TD></TR> <TR class=td2> <TD align=right>链接网址:</TD> <TD><INPUT id=url value=http:// maxLength=50 size=50 name=url> </TD></TR> <TR class=td2> <TD align=right>是否显示:</TD> <TD> <input name="isshow" type="radio" value="1" checked> 是 <input name="isshow" type="radio" value="0"> 否 </TD> </TR> <TR class=td2> <TD align=right>显示顺序:</TD> <TD><INPUT id=sort value=1 maxLength=50 size=10 name=sort> </TD></TR> <TR class=td1> <TD width="15%" align=right>简介:</TD> <TD width="85%"><TEXTAREA id=info rows=3 cols=60 name=info></TEXTAREA></TD></TR></TBODY></TABLE> <TABLE border=0 cellSpacing=0 cellPadding=0 width="100%"> <TBODY> <TR> <TD height=30> </TD></TR></TBODY></TABLE> <TABLE border=0 cellSpacing=0 cellPadding=0 width="100%"> <TBODY> <TR> <TD height=30 align=center><SPAN class=td2> <INPUT class=button_new value="提 交" type=submit name=bt1> <INPUT class=button_new value="重 置" type=reset name=Submit22> </SPAN></TD> </TR></TBODY></TABLE></TD></TR></TBODY></TABLE></FORM></BODY></HTML>