微信小程序使用thinkphp批量上传图片代码


微信小程序使用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);
    }






注:本文转载自拾图网[http://www.tshinet.com],转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除。
上一篇 下一篇

淘宝热销

  • Mistine小黄帽防晒霜乳SPF50泰版面部防紫外线隔离男女军训蜜丝婷

    【推荐理由】赠运费险

    【优惠券】20   【券后价】59.00

    【下单链接】18¥ CZ0001 G5fHdEqOsnB¥

    >>>>>> 马上打开

  • G7旗舰店越南进口美式纯黑咖啡粉速溶无糖0脂减燃正品学生提神

    【推荐理由】库存紧张,好评过万

    【优惠券】3    【券后价】26.90

    【下单链接】27¥ CZ0001 zb0bdEqLV7G¥

    >>>>>> 马上打开