page({

    listimg1:[],
    listimg:[]

})

choose: function () {//这里是选取图片的方法

    var that = this,
    pics = [];
    listimg1 = [];
    var listimgurl = this.data.listimg;
    wx.chooseImage({
        count: 9 - pics.length, // 最多可以选择的图片张数,默认9
        sizeType: ['original', 'compressed'], // original 原图,compressed 压缩图,默认二者都有
        sourceType: ['album', 'camera'], // album 从相册选图,camera 使用相机,默认二者都有
        success: function (res) {
        var imgsrc = res.tempFilePaths;
        pics = pics.concat(imgsrc);
        that.setData({
        pics: pics,

    });
    //开始上传
    uploadimg({
    url: 'https://weixin.cutiemaster.com/uploadxcx.php',//这里是你图片上传的接口
    path: pics//这里是选取的图片的地址数组
    }, that);
},
    fail: function () {
    // fail
    },
complete: function (e) {
        // complete
        //显示图片
        //当前日期拼接文件路径
        var myDate = new Date();
        var year = myDate.getFullYear();
        var m = myDate.getMonth() + 1;
        if (m <= 9) {
            var m = '0' + m;
        }
        var d = myDate.getDate();
        var pathimg = 'wwww.baidu.com/upload/xiaochengxu/' + year + m + d + '/';
        var listimg = [];
        var tempFiles = e.tempFiles;

        for (var i = 0; i < tempFiles.length; i++) {
            listimg.push(tempFiles[i].path)
        }

        that.setData({
            listimg: listimg,
        });
    }
    });

},

///////////////////////////////////写在page外面

var listimg1 = [];
function uploadimg(data, that2) {
    var that = this,
    i = data.i ? data.i : 0,//当前上传的哪张图片
    success = data.success ? data.success : 0,//上传成功的个数
    fail = data.fail ? data.fail : 0;//上传失败的个数
wx.uploadFile({
    url: data.url,
    filePath: data.path[i],
    name: 'file',//这里根据自己的实际情况改
    formData: null,//这里是上传图片时一起上传的数据
    //success:c,
success: (resp) => {
    success++;//图片上传成功,图片上传成功的变量+1
    //这里可能有BUG,失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时,这里的success才+1
    if (resp.statusCode == 200) {
    var pics = resp.data;
    var str = JSON.parse(pics);
    var listpics = str.url;
    listimg1.push(listpics)
    that2.setData({
        listimg1: listimg1
    });
    }
},
    fail: (res) => {
    fail++;//图片上传失败,图片上传失败的变量+1
        console.log('fail:' + i + "fail:" + fail);
    },
    complete: () => {
        i++;//这个图片执行完上传后,开始上传下一张
        if (i == data.path.length) {   //当图片传完时,停止调用  
        console.log('执行完毕');
        console.log('成功:' + success + " 失败:" + fail);
        } else {//若图片还没有传完,则继续调用函数
        data.i = i;
        data.success = success;
        data.fail = fail;
        uploadimg(data, that2);
    }
    
    }
    });

}

////php


   $imgname = $_FILES['file']['name'];
   $tmp = $_FILES['file']['tmp_name'];
   $filepath = 'upload/xiaochengxu/'.date('Ymd').'/';//要自己创建这个文件夹

   if(!file_exists(ROOTPATH.'/'.$filepath)){//文件路径
      mkDirs(ROOTPATH.'/upload/xiaochengxu/'.date('Ymd'));
   }
   if(move_uploaded_file($tmp,$filepath.$imgname)){
      $info['status'] = 1;
      $info['url'] = $filepath.$imgname;
   }else{
      $info['status'] = 0;
   }
   echo json_encode($info);
exit();


今日吐槽

让烦恼与风去远行;让痛苦陪云去流浪。哼一曲好歌,也许心情,就会轻松许多

Contact ME

TAG Link