图片上传预览

来源:互联网 发布:霍华德新秀赛季数据 编辑:程序博客网 时间:2024/05/30 05:08

1、导入jQuery库

<script src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js" type="text/javascript"></script>
2、编写html

<input type="file" name="pimage" id="file0"/>
<img src="" id="img0" >

3、编写js

<script type="text/javascript">

$("#file0").change(function(){

var objUrl = getObjectURL(this.files[0]) ;
console.log("this.files[0]-----"+this.files[0]);
console.log("objUrl = "+objUrl) ;
if (objUrl) {
$("#img0").attr("src", objUrl) ;
}
}) ;
//建立一個可存取到該file的url
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 ;


</script>


0 0
原创粉丝点击