`
陈静静2014
  • 浏览: 40861 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

上传图片预览

    博客分类:
  • Ajax
js 
阅读更多
预览功能的基本设计思路:创建一个img元素,再把文件域的value值赋值给img元素的src属性。
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	
	<script type="text/javascript">
	function showPicture(){
	document.getElementById("img").src=document.form1.picturePath.value;
	}
	</script>
  </head>
  
  <body>
 <form name="form1" action="PicturServlet" method="post">
 <table>
 <tr>
 
 <td><img src=""  name="img" id="img" height="200px"/></td>
 
 </tr>
 <tr>
 <td><input type="file" name="picturePath" onchange="showPicture()"/></td>
 <td><input type="submit" value="上传图片"></td>
 </tr>
 
 </table>
 </form>
  </body>
</html>

 如果用的是Firefox(或Opera),可能会发现什么也没有发生,Firefox的安全策略不允许我们显示一个用户的本地图像文件。
让Firefox显示本地文件的唯一办法就是修改它的默认安全策略:
在Firefox的地址栏中输入“about:config”
在列表中找到“security.checkloaduri” 双击把它的值由true改为false 。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics