apicloud如何上传图片

这个demo是关于如何上传头像,上传的时候注意,点击上传图片

如果照相机调取不到,可以检查手机设置里面,权限设置是否打开(照相机权限)
html
1
<div class="vehicle_type2 "> 2 <div class="vehicle_type_title_head">头像</div> 3 <img src="" alt="" id="img_p" class="img_head "> 4 <input type="text" placeholder="" readonly class="vehicle_type_value_head" onclick="getPicture()" > 5 </div>

 

 js
1
function getPicture(){ 2 //弹框选择拍照,还是手机相册,还是取消上传 3 api.actionSheet({ 4 title : '上传照片', 5 cancelTitle : '取消', 6 buttons : ['拍照', '手机相册'] 7 8 }, function(ret, err) { 9 var index = ret.buttonIndex; 10 if (ret) { 11 // 如果ret.buttonIndex == 1通过调取照相机拍照,如果照相机调取不到可以检查手机设置里面,权限设置是否打开(照相机权限) 12 if (ret.buttonIndex == 1) { 13 api.getPicture({ 14 sourceType : 'camera', 15 encodingType : 'jpg', 16 mediaValue : 'pic', 17 destinationType : 'url', 18 allowEdit : false, 19 quality : 100, 20 21 saveToPhotoAlbum : false 22 }, function(ret, err) { 23 // console.log(JSON.stringify(ret.data)); 24 if (ret) { 25 //将上传图片返回的路径赋值给head_pic 26 // head_pic = ret.data; 27 // 通过$api.attr();方法给id为img_p 附上路径ret.data,图片显示在页面上 28 $api.attr(img_p,'src', ret.data); 29 img_pic1 = ret.data; 30 // 将图片上传给后台 31 api.ajax({ 32 url: 'http://xxxxx/index/system/uploadImg', 33 method: 'post', 34 processData:false, 35 contentType: false, 36 data: { 37 files: { 38 image: img_pic1 39 } 40 } 41 },function(ret, err){ 42 if (ret) { 43 image = ret.data; 44 var user_id= $api.getStorage('user_id'); 45 // 后台转译过得路径传给提交图片的接口 46 api.ajax({ 47 url: 'http://xxxxxxxxx/index/user/editUserInfo', 48 method: 'post', 49 data: { 50 values: { 51 user_id: user_id, 52 headimg:image, 53 54 } 55 } 56 },function(ret, err){ 57 if (ret) { 58 59 console.log(JSON.stringify(ret)); 60 61 62 } else { 63 alert( JSON.stringify( err ) ); 64 } 65 }); 66 } else { 67 alert( JSON.stringify( err ) ); 68 } 69 }); 70 71 72 } else { 73 api.toast({ 74 msg : '图像获取失败', 75 duration : 3000, 76 location : 'bottom' 77 }); 78 } 79 }); 80 } else if (ret.buttonIndex == 2) { 81 // 如果ret.buttonIndex == 2手机相册选图片 82 83 api.getPicture({ 84 sourceType : 'library', 85 encodingType : 'png', 86 mediaValue : 'pic', 87 destinationType : 'url', 88 allowEdit : true, 89 quality : 100, 90 preview:true, 91 saveToPhotoAlbum : false 92 }, function(ret, err) { 93 if (ret) { 94 95 $api.attr(img_p,'src', ret.data); 96 img_pic1 = ret.data; 97 console.log(img_pic1); 98 api.ajax({ 99 url: 'http://xxxxxxxxxxx/index/system/uploadImg', 100 method: 'post', 101 processData:false, 102 contentType: false, 103 data: { 104 files: { 105 image: img_pic1 106 } 107 } 108 },function(ret, err){ 109 if (ret) { 110 111 image = ret.data; 112 console.log(image); 113 console.log(img_pic1); 114 var user_id= $api.getStorage('user_id'); 115 api.ajax({ 116 url: 'http://xxxxx/index/user/editUserInfo', 117 method: 'post', 118 data: { 119 values: { 120 user_id: user_id, 121 headimg:image, 122 123 } 124 } 125 },function(ret, err){ 126 if (ret) { 127 128 console.log(JSON.stringify(ret)); 129 130 131 } else { 132 alert( JSON.stringify( err ) ); 133 } 134 }); 135 } else { 136 alert( JSON.stringify( err ) ); 137 } 138 }); 139 140 } else { 141 api.toast({ 142 msg : '图像获取失败', 143 duration : 3000, 144 location : 'bottom' 145 }); 146 } 147 }); 148 149 } 150 } 151 }); 152 };
原文地址:https://www.cnblogs.com/zfdbk/p/10256130.html