<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" media="(device-height: 568px)">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">
<title>jqeury H5 拍照上传</title>
<script src="js/jquery.min.js"></script>
<style>
.imageDiv {
display:inline-block;
width:100px;
height:100px;
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
box-sizing:border-box;
border:1px dashed darkgray;
background:#f8f8f8;
position:relative;
overflow:hidden;
margin:10px
}
.cover {
position:absolute;
z-index:1;
top:0;
left:0;
width:100px;
height:100px;
background-color:rgba(0,0,0,.3);
display:none;
line-height:125px;
text-align:center;
cursor:pointer;
}
.cover .delbtn {
color:red;
font-size:10px;
}
.imageDiv:hover .cover {
display:block;
}
.addImages {
display:inline-block;
width:100px;
height:100px;
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
box-sizing:border-box;
border:1px dashed darkgray;
background:#f8f8f8;
position:relative;
overflow:hidden;
margin:10px;
}
.text-detail {
margin-top:10px;
text-align:center;
}
.text-detail span {
font-size:30px;
}
.file {
position:absolute;
top:0;
left:0;
width:100px;
height:100px;
opacity:0;
}
</style>
</head>
<body>
<div style="width: 70%;margin: 0 auto">
<div id="Pic_pass">
<p style="font-weight: bold;">拍照或者手动上传图片</p>
<p><span style="color: red">注:每张图片大小不可超过4M,一次最多可以上传4张</span></p>
<div class="picDiv">
<div class="addImages">
<input type="file" class="file" name="file[]" id="fileInput" multiple="multiple" accept="image/png, image/jpeg, image/gif, image/jpg">
<div class="text-detail">
<span>+</span>
<p>点击上传</p>
</div>
</div>
</div>
</div>
<div class="msg" style="display: none;"></div>
</div>
<script>
//图片上传预览功能
var userAgent = navigator.userAgent; //用于判断浏览器类型
$(".file").change(function() {
//获取选择图片的对象
var docObj = $(this)[0];
var picDiv = $(this).parents(".picDiv");
//得到所有的图片文件
var fileList = docObj.files;
//console.log(fileList);
//循环遍历
for (var i = 0; i < fileList.length; i++) {
UpLoadImg(fileList[i]);
//动态添加html元素
var picHtml = "<div class='imageDiv' > <img id='img" + fileList[i].name + "' /> <div class='cover'><i class='delbtn'>删除</i></div></div>";
// console.log(picHtml);
picDiv.prepend(picHtml);
//获取图片imgi的对象
var imgObjPreview = document.getElementById("img" + fileList[i].name);
if (fileList && fileList[i]) {
//图片属性
imgObjPreview.style.display = 'block';
imgObjPreview.style.width = '100px';
imgObjPreview.style.height = '100px';
//imgObjPreview.src = docObj.files[0].getAsDataURL();
//火狐7以上版本不能用上面的getAsDataURL()方式获取,需要以下方式
if (userAgent.indexOf('MSIE') == -1) {
//IE以外浏览器
imgObjPreview.src = window.URL.createObjectURL(docObj.files[i]); //获取上传图片文件的物理路径;
// console.log(imgObjPreview.src);
// var msgHtml = '<input type="file" id="fileInput" multiple/>';
} else {
//IE浏览器
if (docObj.value.indexOf(",") != -1) {
var srcArr = docObj.value.split(",");
imgObjPreview.src = srcArr[i];
} else {
imgObjPreview.src = docObj.value;
}
}
}
}
/*删除功能*/
$(".delbtn").click(function() {
var _this = $(this);
_this.parents(".imageDiv").remove();
});
});
function UpLoadImg(f){
//获取上传文件
var formData = new FormData();
formData.append('img_url', f);
//console.log(formData)
//提交后台处理
$.ajax({
url: 'a.php?action=1',
type: 'POST',
data: formData,
dataType: "JSON",
processData: false,
contentType: false
}).done(function(res) {
console.log(res.url);
if(res.status == 1){
//赋值给字段
// $('#url_data').val(res.url);
// alert(res.msg)
}else{
// alert(res.msg)
}
}).fail(function(res) {
});
}
</script>
</body>
</html>
评论0