<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图片预览上传</title>
<style type="text/css">
img {
200px;
height: 200px;
}
#uploadFile {
display: block;
200px;
height: 200px;
}
.policeMan_img{
200px;
height: 200px;
border: 1px solid blueviolet;
}
.imgtis{
display: none;
color: red;
}
</style>
</head>
<body>
<div id="">
<div class="policeMan_img">
<img class="uploadImg" style="display:none" />
</div>
<div class="imgtis">所选取图片的格式不对!</div>
<input id="file_upload" type="file" name="file_upload" class="" onchange="showPic()" >
</div>
<script src="js/jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
function showPic() {
var pic = $("#file_upload").get(0).files[0];
// console.log(pic)
if(typeof(pic)!=undefined && typeof(pic)!="undefined" ){
$(".uploadImg").prop("src", window.URL.createObjectURL(pic));
$(".uploadImg").css("display", "block");
var imgPath = $("#file_upload").val();
// console.log(imgPath)
if(imgPath != "") {
var str = imgPath.substr(imgPath.lastIndexOf('.') + 1);
if(str != 'jpg' && str != 'png' && str != 'JPG' && str != 'PNG') {
//alert("选取的图片格式不对");
$(".imgtis").css("display", "block");
} else {
$(".imgtis").css("display", "none");
}
}
}else{
// $(".uploadImg").css("display", "none");
$(".uploadImg").removeProp("src")
}
}
</script>
</body>
</html>