标签:
//1.检查图像接口 document.querySelector(‘#chooseImage‘).onclick = function() { log_msg(‘检查api‘); unvaild_api = []; wx.checkJsApi({ jsApiList:[ ‘chooseImage‘, ‘previewImage‘, ‘uploadImage‘, ‘downloadImage‘, ], success:function(res){ log_msg(‘返回检查api的结果‘); log_msg(‘1.判断检查api的结果‘); $.each(res.checkResult, function(k, v) { if (!v) { unvaild_api.push(k); return false; } }); if (unvaild_api.length > 0) { log_msg(‘存在无效api‘+unvaild_api.join(‘, ‘)); log_msg(‘存在无效api的结果‘+unvaild_api.join(‘, ‘)); return false; } log_msg(‘无无效api‘); var images = { localIds: [], serverId: [] }; //2.选择本地或拍照的图片 wx.chooseImage({ success: function (res) { log_msg("返回选定照片的本地ID列表或拍照图片ID"+JSON.stringify(res)); images.localIds = res.localIds; log_msg(‘已选择 ‘ + images.localIds.length + ‘ 张图片‘); log_msg(‘图片地址: ‘ + images.localIds.join(‘, ‘)); //3.上传选择的图片(递归) var i = 0, length = images.localIds.length; var imgs_html = []; var upload = function(){ log_msg("上传图片第"+i+‘图片‘); wx.uploadImage({ localId:images.localIds[i], success: function(res) { log_msg(‘记录res.serverId‘+res.serverId); images.serverId.push(res.serverId); imgs_html.push(‘<img src="‘+images.localIds[i]+‘"/>‘); log_msg(‘已上传‘+images.localIds[i]+i+‘/‘+length); //如果还有照片,继续上传 i++; if (i < length) { upload(); } else { //4.预览上传的图片 $("#img_wrap").html(imgs_html.join(‘‘)); log_msg("上传成功"); } } }); }; log_msg("开始上传图片"); upload(); } }); } }); }
//4.显示下载后的相片 var img_html = ‘‘; i = 0;//重置i var download = function(){ wx.downloadImage({ serverId:images.serverIds[i], success:function(res){ log_msg(‘下载第‘+i+‘张图片的结果:‘+JSON.stringify(res)); img_html += ‘<img src="‘+res.localId+‘" />‘; //如果还有下载的图片,继续下载 i++; if (i < length) { download(); } else{ $("#img_wrap").html(img_html); } } }); } log_msg("开始下载图片"); download();
demo地址:http://jdhu.sinaapp.com
其他:
var log_msg = function(msg){ $.post(‘<?=site_url("log/index")?>‘, {msg:msg}); }
class Log extends SAE_Controller { public function index() { $msg = $this->input->post(‘msg‘); log_message(‘error‘, $msg); echo $msg; } }
标签:
原文地址:http://www.cnblogs.com/jdhu/p/4218315.html