apicloud 上傳頭像或圖片並預覽
阿新 • • 發佈:2019-01-02
點選頭像自動上傳
<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("失敗") } }) }