1
HTML部分
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<title>头像上传</title>
<link rel="stylesheet" href="../../css/api.css" />
<style type="text/css">
.box{
width: 80px;
height: 80px;
border-radius: 100%;
border: 1px solid #ccc;
overflow: hidden;
}
.box img{
width: 100%;
height: 100%;
}
</style>
<head/>
<body>
<div>
<img src ='' id='wode'>
</div>
</body>
javascript部分
<script type="text/javascript" >
//调用摄像头或者手机相册,保证启用摄像权限,如果未开启权限,否则会报错,
//如果需要开启权限,请假对应的判断语句
function uploadHeadPic() {
api.actionSheet({
title : '上传照片',
cancelTitle : '取消',
buttons : ['拍照', '手机相册']
}, function(ret, err) {
if (ret) {
if (ret.buttonIndex == 1) {
api.getPicture({
sourceType : 'camera',
encodingType : 'jpg',
mediaValue : 'pic',
destinationType : 'url',
allowEdit : false,
quality : 100,
saveToPhotoAlbum : false
}, function(ret, err) {
// alert(JSON.stringify(ret));
if (ret) {
saveImg(ret.data);
} else {
api.toast({
msg : '图像获取失败',
duration : 3000,
location : 'bottom'
});
}
});
} else if (ret.buttonIndex == 2) {
//手机相册选图片
api.getPicture({
sourceType : 'library',
encodingType : 'png',
mediaValue : 'pic',
destinationType : 'url',
allowEdit : true,
quality : 100,
preview:true,
saveToPhotoAlbum : false
}, function(ret, err) {
//返回选择或者拍摄后的路径
// alert(JSON.stringify(ret.data));
if (ret) {
saveImg(ret.data);
} else {
api.toast({
msg : '图像获取失败',
duration : 3000,
location : 'bottom'
});
}
});
}
}
});
};
//保存剪辑图像
function saveImg(path) {
api.showProgress({
title: '上传中...',
text: '先喝杯茶...',
});
//上传剪辑后的图像到服务器
api.ajax({
// report : false,
url : uploadImageUrl,
method : 'post',
cache : 'false',
timeout : 30,
dataTpye : 'json',
data : {
files : {
file : path
}
}
}, function(ret, err) {
//服务器返回上传后的回调信息,
//alert(JSON.stringify(ret));
api.hideProgress();
//这里根据不同的例子来做不同的调整
//获取到img节点,并且将src替换,
var element = document.getElementById('wode');
//我这里后端返回的数据,图片字段就是ok
element.src = ret.ok;
});
};
</script>