注意:實現這個效果最重要的就是獲取到用戶上傳到網頁上的真實路徑。
關鍵代碼:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
function getObjectURL(file){
var url=null
if(window.createObjectURL!=undefined){ // basic
url=window.createObjectURL(file)
}else if(window.URL!=undefined){ // mozilla(firefox)
url=window.URL.createObjectURL(file)
} else if(window.webkitURL!=undefined){ // webkit or chrome
url=window.webkitURL.createObjectURL(file)
}
return url ;
}
$("#face_upload").change(function(){
var objUrl=getObjectURL(this.files[0]);
var size=this.files[0].size;
if(size>=1024000*10)bottomTip("圖片超過10M了哦",0);
else{
if(objUrl){
$("#xs").attr("src",objUrl);
}
}
}) ;
})
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body><a href="http://s.jf3q.com">click</a>
<form enctype="multipart/form-data">
商品圖片 <input type="file" id="face_upload"/><br>
<img src="" height="200" width="200" id="xs"/>
</form>
</body>
</html>
最後的效果如下:
或者微信:fyydbc