【推荐理由】赠运费险
【优惠券】20 【券后价】59.00
【下单链接】18¥ CZ0001 G5fHdEqOsnB¥
微信小程序使用thinkphp批量上传图片代码,分为客户的小程序代码,和 服务端的php代码,现在温州网站建设把关键代码拿出来,跟大家分享下:
首先看,小程序的代码:
data: { nickName: "", avatarUrl: "", gender: "", province: "", city: "", country: "", upload_picture_list:[], imageList: [], sourceTypeIndex: 2, sourceType: ['拍照', '相册', '拍照或相册'], sizeTypeIndex: 2, sizeType: ['压缩', '原图', '压缩或原图'], countIndex: 2, count: [1, 2, 3, 4, 5, 6, 7, 8, 9] }, chooseImage: function () { var that = this var upload_picture_list = that.data.upload_picture_list; wx.chooseImage({ sourceType: ['album', 'camera'], sizeType: ['original', 'compressed'], count:3, success: function (res) { console.log(res) var tempFiles = res.tempFiles; //循环把图片加入到上传列表 for (var i in tempFiles) { tempFiles[i]['upload_percent']=0; tempFiles[i]['path_server']=''; upload_picture_list.push(tempFiles[i]); } that.setData({ upload_picture_list: upload_picture_list, }); //------------------循环把图片上传到服务器 并显示进度 for (var j in upload_picture_list) { if (upload_picture_list[j]['upload_percent']==0) { upload_file_server(that, upload_picture_list,j); } } } }) //-------------------------------------------------------------------------------------------begin 函数 upload_file_server function upload_file_server(that, upload_picture_list, j) { //console.log("开始上传" + j + "图片到服务器"); //console.log(upload_picture_list[j]); var upload_task = wx.uploadFile({ url: config.api.SubPics, filePath: upload_picture_list[j]['path'], name: 'file', formData: { 'path': 'wxchat' },//附件数据,这里为路径 success: function (res) { var data = JSON.parse(res.data);//字符串转化为json; if (data.success == true) { var filename = "" + data.savename; upload_picture_list[j]['path_server'] = filename; } that.setData({ upload_picture_list: upload_picture_list }); console.log("图片上传" + j + "到服务器完成"); //console.log(upload_picture_list[j]); } }) upload_task.onProgressUpdate((res) => { upload_picture_list[j]['upload_percent'] = res.progress // console.log("第" + j + "个图片上传进度:" + upload_picture_list[j]['upload_percent']); //console.log(upload_picture_list); that.setData({ upload_picture_list: upload_picture_list }) }) } //-------------------------------------------------------------------------------------------end 函数 upload_file_server },
服务器上的php代码:
public function savePics() { $config=array( 'rootPath' => './'.C("UPLOADPATH"), 'savePath' => './zhengshu/', 'maxSize' => 512000,//500K 'saveName' => array('uniqid',''), 'exts' => array('jpg', 'png', 'jpeg'), 'autoSub' => false, ); $upload = new \Think\Upload($config,'Local');//先在本地裁剪 $info=$upload->upload(); //开始上传 if (!$info) { //上传失败,返回错误 $data["message"]=$upload->getError(); $data['success']=false; } else { //上传成功 foreach ($info as $file) { $filepath=C("MINIHTTPS")."data/upload/zhengshu/".$file['savename']; $data['success']=true; $data['savename']=$filepath; } } $this->ajaxReturn($data); }