在上一些网站时我们需要提交图片或者一些证件图片方便用户查看是否上传错误,或者看看效果,比如在QQ上上传头像,在一些官方网站需要上传照片文件审核。
前端代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
//alert("nihao1");
$("#picture").change(function() {
var current_pic=this.files[0];
//alert("current_pic="+current_pic);
preview_picture(current_pic);
});
});
function preview_picture(pic) {
//alert("你好!");
var r=new FileReader();
r.readAsDataURL(pic);
r.onload=function (){
//alert("你好123!");
$(".content > div > img").attr("src",this.result).show();
//alert("你好321!");
};
}
</script>
</head>
<body>
<div class="content">
<div>
上传图片:<input class="btn btn-danger" id="picture" type="file" >
</div>
<div>
<img src="" >
</div>
</div>
</body>
</html>
需要引入
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
在程序启动时需要加载
$(document).ready(function() {
//alert("nihao1");
$("#picture").change(function() {
var current_pic=this.files[0];
//alert("current_pic="+current_pic);
preview_picture(current_pic);
});
});
在其中需要实现方法
function preview_picture(pic) {
//alert("你好!");
var r=new FileReader();
r.readAsDataURL(pic);
r.onload=function (){
//alert("你好123!");
$(".content > div > img").attr("src",this.result).show();
//alert("你好321!");
};
}
![](https://img.haomeiwen.com/i2367321/8dd313ab3159c1b8.png)
网友评论