apicloud上传图片到云端数据库(荐) 作者:马育民 • 2019-03-30 11:34 • 阅读:10138 ### html代码: ```shell ``` ### 引入相关js: - api.js - APICloud-rest-SHA1.js - jquery.js ### js代码: ``` function selectPhone(){ api.actionSheet({ title: '上传头像', cancelTitle: '取消', buttons: ['拍照','从手机相册选择'] }, function(ret, err) { if (ret) { getPicture(ret.buttonIndex); } }); } function getPicture(sourceType){ if(sourceType==1){ // 拍照 //获取一张图片 api.getPicture({ sourceType: 'camera', encodingType: 'png', mediaValue: 'pic', allowEdit: false, quality: 90, saveToPhotoAlbum: true }, function(ret, err) { // 获取拍照数据并处理 if (ret) { var imgSrc = ret.data; if (imgSrc != "") { $('#avatar').attr('src',ret.data); updateFile(ret.data,""); } } }); }else if(sourceType==2){ // 从相机中选择 api.getPicture({ sourceType: 'album', encodingType: 'jpg', mediaValue: 'pic', destinationType: 'url', allowEdit: true, quality: 50, targetWidth: 100, targetHeight: 100, saveToPhotoAlbum: false }, function(ret, err) { if (ret) { alert(JSON.stringify(ret)); $('#avatar').attr('src',ret.data); updateFile(ret.data,""); } else { alert(JSON.stringify(err)); } }); } } /* * 图片上传 */ function updateFile(path,filename){ var client = new Resource("A6050753711177", "62DA62DE-10BB-D1FE-55B0-B335FEA94510"); var File = client.Factory("file"); File.save( { file: { isFile:true, path:path, values: { filename:filename } } }, function(data,err){ api.hideProgress(); if(data){ alert("上传结果:"+JSON.stringify(data)); }else{ alert("上传发生错误:"+JSON.stringify(err)); } }) } ``` 原文出处:https://malaoshi.top/show_1EF33frYp4sO.html