html+css+js实现拍照预览上传图⽚功能
前⾔:我们在做⽹页时经常会需要有上传图⽚的需求,可能是选择图⽚或者拍照上传,如果简单的使⽤<input type="file"/>这种⽅式虽然也能实现功能,但⽤户体验上可能会差了⼀些,所以本⽂记录了使⽤css+js实现图⽚选中后的预览及压缩上传功能,部分带来来源于⽹络,此处做了记录整理。
效果预览:
1.创建index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>拍照上传</title>
<link rel="stylesheet" href="index.css"/>
<script type='text/javascript' src='index.js' charset='utf-8'></script>
</head>
<body>
<form id="mainForm">
<div class="content">
css和html和js怎么结合<div class="label">⾝份证</div>
<div class="img-area">
<div class="container">
<input type="file" id='id-face' name='face' accept="image/*" />
<div id='face-empty-result'>
<img style='width:4rem' src="github/wangheng3751/my-resources/blob/master/images/camera.png?raw=true" alt="">
<p>⾝份证正⾯照</p>
</div>
<img style='width: 100%' id='face-result'/>
</div>
<div class="container" style='margin-top:0.5rem;'>
<input type="file" id='id-back' name='back' accept="image/*" />
<div id='back-empty-result'>
<img style='width:4rem' src="github/wangheng3751/my-resources/blob/master/images/camera.png?raw=true" alt="">
<p>⾝份证反⾯照</p>
</div>
<img style='width: 100%' id='back-result'/>
</div>
</div>
</div>
<div class="btn">
提交
</div>
</form>
</body>
</html>
2.创建index.css
body{
margin: 0
}
.content{
padding:0.5rem;
display: flex;
align-items: center;
border-bottom: 1px #999 solid
}
.label{
width:5rem;
}
.img-area{
flex:1
}
.
container{
background-color:#e7e7e7;
position: relative;
}
.container div{
text-align: center;
padding:0.5rem 0
}
.container input{
opacity:0;
filter:alpha(opacity=0);
height: 100%;
width: 100%;
position: absolute;
top: 0;
left: 0;
z-index: 9;
}
.container p{
font-size: 0.9rem;
color:#999
}
.
btn{
background-color: #4363ab;
color: #fff;
text-align: center;
padding: 0.5rem 1rem;
width:80%;
border-radius: 0.2rem;
margin: 2rem auto;
font-weight: 600;
font-size: 1.2rem
}
3.创建index.js
onFileChange(this,"face-result","face-empty-result")
});
onFileChange(this,"back-result","back-empty-result")
});
});
};
/
**
* 选中图⽚时的处理
* @param {*} fileObj input file元素
* @param {*} el //选中后⽤于显⽰图⽚的元素ID
* @param {*} btnel //未选中图⽚时显⽰的按钮区域ID
*/
function onFileChange(fileObj,el,btnel){
var windowURL = window.URL || window.webkitURL;
var dataURL;
var imgObj = ElementById(el);
imgObj.style.display="block";
if (fileObj && fileObj.files && fileObj.files[0]) {
dataURL = ateObjectURL(fileObj.files[0]);
imgObj.src=dataURL;
} else {
dataURL = fileObj.value;
imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL;
}
}
/**
* 将图⽚压缩后返回base64格式的数据
* @param {*} image img元素
* @param {*} width 压缩后图⽚宽度
* @param {*} height 压缩后图⽚⾼度
* @param {*} qua //图⽚质量1-100
*/
function compressImageTobase64(image,width,height,qua){
var quality = qua ? qua / 100 : 0.8;
var canvas = ateElement("canvas"),
ctx = Context('2d');
var w = image.naturalWidth,
h = image.naturalHeight;
canvas.width = width||w;
canvas.height = height||h;
ctx.drawImage(image, 0, 0, w, h, 0, 0, width||w, height||h);
var data = DataURL("image/jpeg", quality);
return data;
}
//提交
function submit(){
//1、form提交
//ElementById("mainForm").submit();
/
/2、压缩后ajax提交
var face_data=ElementById("face-result"),200,100,90);
var back_data=ElementById("back-result"),200,100,90);
var formData = new FormData();
formData.append("face",face_data);
formData.append("back",back_data);
//需引⼊jQuery
$.ajax({
url:"/地址",
type: 'POST',
cache: false,
data: formData,
timeout:180000,
processData: false,
contentType: false,
success:function(r){
},
error:function(r){
}
});
}
源码:
总结
以上所述是⼩编给⼤家介绍的html+css+js 实现拍照预览上传图⽚功能,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论