项目实训八,图片上传

将图片转化为base64数组,上传

const app = getApp()
Page({
  data: {
    showtab:0,  //顶部选项卡索引
    showtabtype:'', //选中类型
    showfootertab:0,  //底部标签页索引
    questionsall:[],  //所有问题
    questions:[], //问题列表
    showquestionindex:null, //查看问题索引,
    uploadimgs:[], //上传图片列表
    editable: true,//是否可编辑
    piyu_value:'',

    id:''
  },
  onLoad: function (option) {
    this.setData({
      id:option.id,
      uploadimgs:[]
    })
  console.log(this.data.id)
  },
    piyu: function(res){
    console.log("输入的值为:"+res.detail.value);//打印输入的值
    this.setData({
      piyu_value: res.detail.value
    })
  },
  chooseImage:function() {
    let _this = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#A10606",
      success: function(res) {
        if (!res.cancel) {
          if(res.tapIndex == 0){
            _this.chooseWxImage('album')
          }else if(res.tapIndex == 1){
            _this.chooseWxImage('camera')
          }
        }
      }
    })
  },
  chooseWxImage:function(type){
    let _this = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        _this.setData({
          uploadimgs: _this.data.uploadimgs.concat(res.tempFilePaths)
        })
      }
    })
  },
  editImage:function(){
    this.setData({
      editable: !this.data.editable
    })
  },
  deleteImg:function(e){
    console.log(e.currentTarget.dataset.index);
    const imgs = this.data.uploadimgs
    Array.prototype.remove = function(i){
      const l = this.length;
      if(l==1){
        return []
      }else if(i>1){
        return [].concat(this.splice(0,i),this.splice(i+1,l-1))
      }
    }
    this.setData({
      uploadimgs: imgs.remove(e.currentTarget.dataset.index)
    })
  },
  questionSubmit:function(){
    var that = this;
    
    var base64=[];
    for(var i = 0; i < that.data.uploadimgs.length; i++)
    {
      base64.push(wx.getFileSystemManager().readFileSync(that.data.uploadimgs[i], "base64"));  
    }
    console.log(base64);
  wx.request({
    url: app.globalData.globalReqUrl +'/api/material/base64',
    data:{
      photo: base64,
      affair_id:that.data.id,
      name:'微信上传',
      remark:that.data.piyu_value  
    },
    method:'POST',
    header:{
      'content-type':'application/x-www-form-urlencoded',
      'token':app.globalData.token
    },
    success:function(res){
      console.log(res);
      let error = null;
      if(res.data.status!="200"){
        
        error = "系统错误 请稍后尝试"
         
      }
      if(error !== null){
        wx.showToast({
          title: error,
          icon:'none',
          duration:2000
        })
       
      }else{
        wx.showToast({
          title:'上传成功',
          icon:'success',
          duration:1000,
          success:function(){
            setTimeout(function(){
              wx.navigateBack({
                delta: 2,
              })
          },1000);
          }
        })    
       
      }
      
    },
    fail:function(res){
      console.log(res);
    
    }
  })
  },
  
})

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值