点击头像自动上传
<div class="head" tapmode onclick="showAction()"><img id="img_src" src="../image/preview.jpg"/></div>
// 点击头像
function showAction() {
api.actionSheet({
title: '上传图片',
cancelTitle: '取消',
buttons: ['拍照', '从手机相册选择']
}, function(ret, err) {
if (ret) {
getPicture(ret.buttonIndex);
}
});
}
function getPicture(sourceType) {
if (sourceType == 1) { // 拍照
api.getPicture({
sourceType: 'camera',
encodingType: 'jpg',
mediaValue: 'pic',
allowEdit: false,
destinationType: 'url',
quality: 100,
targetWidth: 200,
targetHeight: 200,
saveToPhotoAlbum: true
}, function(ret, err) {
if(ret){
api.showProgress({
style: 'default',
animationType: 'fade',
text: '图片上传中...',
modal: false
});
document.getElementById("img_src").src = ret.data;//图片链接
uploading(ret.data)
}
});
} else if (sourceType == 2) { // 从相机中选择
api.getPicture({
sourceType: 'library',
encodingType: 'jpg',
mediaValue: 'pic',
destinationType: 'url',
quality: 100,
targetWidth: 200,
targetHeight: 200
}, function(ret, err) {
if(ret){
api.showProgress({
style: 'default',
animationType: 'fade',
text: '图片上传中...',
modal: false
});
document.getElementById("img_src").src = ret.data;//图片链接
uploading(ret.data)
}
});
}else{
return;
}
}
//上传
function uploading(avatar){
api.ajax({
url:'url',
method: 'post',
headers: {//请求头
Authorization: token
},
data: {
files : {//图片链接上传
voucher : avatar
},
values: {//上传参数
"uid":uid,
}
}
},function(ret, err){
if (ret) {
console.log("成功")
}else{
console.log("失败")
}
})
}