过去项目的需求:
1、上传身份证照片时显示上传时的进度效果;
2、上传结束后预览照片;
先附上图:
完整案例下载地址:http://download.csdn.net/download/webtoy/9970435
思路:
1、引入jquery.form.js插件;
2、ajaxSubmit上传以及canvas画圈,在ajaxSubmit的uploadProgress方法中 传参关联上传进度,上传成功后设置照片路径。
html代码如下:
<div class='uploadWrap'>
<div class="uploadIDcard">
<div class="upload_box">
<ul>
<li id='li_idCard'>
<span class="percent" id="percent1">+span>
<canvas class="process" id="process1">0%canvas>
<input type="file" id="IDCardFront" name="IDCardFront">
<img id='img' src="">
li>
ul>
div>
div>
div>
CSS
body{
text-align: center;
}
.uploadWrap{
margin: auto;
width: 500px;
height: 500px;
}
#li_idCard{
width: 210px;
height: 120px;
background: #b3dde2;
color: #fff;
text-align: center;
line-height: 120px;
font-size: 30px;
border: 1px solid #999;
position: relative;
}
#IDCardFront{
width: 210px;
height: 120px;
opacity: 0;
position: absolute;
right: 0;
top: 0;
cursor: pointer;
}
.process {
width: 280px;
height: 140px;
position: absolute;
top: 30%;
left: 40%;
display: none;
}
#img{
margin: 0 auto;
margin-top: 200px;
}
js
//进度条渲染效果
function drawProcess(id) {
var text = $("#" + id).text();
var process = text.substring(0, text.length - 1);
var canvas = $("#" + id);
var context = canvas[0].getContext('2d');
context.clearRect(0, 0, 48, 48);
context.beginPath();
context.moveTo(240, 240);
context.arc(24, 24, 24, 0, Math.PI * 2, false);
context.closePath();
context.fillStyle = '#fff';
context.fill();
context.beginPath();
context.moveTo(24, 24);
context.arc(24, 24, 24, 0, Math.PI * 2 * process / 100, false);
context.closePath();
context.fillStyle = 'rgb(234,117,96)'; //圈的百分比
context.fill();
context.beginPath();
context.moveTo(24, 24);
context.arc(24, 24, 21, 0, Math.PI * 2, true);
context.closePath();
context.fillStyle = 'rgba(255,255,255,1)';
context.fill();
context.beginPath();
context.arc(24, 24, 18.5, 0, Math.PI * 2, true);
context.closePath();
context.strokeStyle = '#ddd';
context.stroke();
context.font = "bold 9pt Arial";
context.fillStyle = 'rgb(234,117,96)'; //百分比数字颜色
context.textAlign = 'center';
context.textBaseline = 'middle';
context.moveTo(24, 24);
context.fillText(text, 24, 24);
}
// 上传事件
$("#IDCardFront").change(function () {
if ($("#FrontUpload").length>0) {
} else {
$("#li_idCard").wrap("");
}
$("#FrontUpload").ajaxSubmit({
dataType: 'json',
beforeSend: function () {
},
//参数不可减少
uploadProgress: function (event, position, total, percentComplete) {
$('#percent1').hide();
$('#process1').show();
$('#process1').text(percentComplete + "%"); //上传进度
drawProcess('process1');
},
success: function (data) {
if (data.result == 'true') {
$('#img').attr('src','./'+data.img+'');
} else {
alert('上传失败');
}
}
});
});
php:
error_reporting(0);//禁用错误报告
if (file_exists("./" . $_FILES["IDCardFront"]["name"])){
$arrRet=array(
'result'=>'false'
);
}else{
$ret=move_uploaded_file($_FILES["IDCardFront"]["tmp_name"],"./" . $_FILES["IDCardFront"]["name"]);
if($ret){
$arrRet=array(
'result'=>'true',
'img'=> $_FILES["IDCardFront"]["name"]
);
}else{
$arrRet=array(
'result'=>'false'
);
}
}
echo json_encode($arrRet);
?>
本篇完。