陈程的技术博客

  • 关于作者
全栈软件工程师
一个专注于技术研究创新的程序员
  1. 首页
  2. 小程序
  3. 正文

小程序-上传图片功能

2020年4月7日 597点热度 0人点赞 0条评论

技术前置:

1.框架采用colorUI

2.原生开发

功能:

上传图片

1.上传已经拍摄的图片

2.实时拍摄上传

3.设置上传图片数量,每次上传数量

4.上传等待

ChooseImage() {
    if(this.data.imgList.length>=4){
      _this.ErrorEvent("最多上传4张照片");
      return;
    }

    if(this.data.imgList.length>=4){
      _this.ErrorEvent("最多上传4张照片");
      return;
    }

    if(this.data.Mylatitude==null){
      this.ErrorEvent("请点击定位送达地址!");
      return;
    }
    if(this.data.Mylongitude==null){
      this.ErrorEvent("请点击定位送达地址!");
      return;
    }

    var position=this.data.Mylatitude+","+this.data.Mylongitude;

    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
      sourceType: ['album','camera'], //从相册选择 ,
      success: (res) => {
        if(res.tempFilePaths.length<=0){
          return;
        }
        wx.showLoading({
          title: '正在上传图片',
         // icon: 'loading',
         // duration: 5000,
          mask: true
        })
        const tempFilePaths = res.tempFilePaths;
        var uploadimgs=[];
        for(var i=0;i<tempFilePaths.length;i++){
          this.uploadImg(tempFilePaths[i],position);
        }
      }
    });
  },
  //上传图片
  uploadImg(address,longlatData){
    let reqaddress= app.globalData.baseUrl+ '/uploadPic';
    let _this=this;
    wx.uploadFile({
      url: reqaddress, //仅为示例,非真实的接口地址
      filePath: address,
      name: 'file',
      formData: {
        'user': 'test'
      },
      success (res){
        const data = res.data;
        var mdata= JSON.parse(data);
        wx.hideLoading();
        if(mdata.code==0)
        {
          if (_this.data.uploadImgList.length > 0) {
            _this.setData({
              uploadImgList:_this.data.uploadImgList.concat(mdata.data.relativePath),
            })
          } else {
            var show="uploadImgList[0]";
            _this.setData({
              [show]:mdata.data.relativePath
            })
          }
          if (_this.data.imgList.length != 0) {
            _this.setData({
              imgList: _this.data.imgList.concat(address),
            })
          } else {
            var show="imgList[0]";
            _this.setData({
              [show]: address,
            })
          }
          _this.HideErrorEvent();
          wx.showToast({
            title: '图片上传成功',
            duration: 1000,
          })
        }
        else
        {
          // var count=_this.data.imgList.length;
          // _this.setData({
          //   imgList:_this.data.imgList.splice(count-1, 1)
          // })
          _this.ErrorEvent("上传图片到服务器失败!请重传");
        }
        
      }, complete() {  
      },
      fail(error){
        wx.hideLoading();
        console.log(error);
        // var count=_this.data.imgList.length;
        // _this.setData({
        //   imgList:_this.data.imgList.splice(count-1, 1)
        // })
        _this.ErrorEvent("上传图片到服务器失败!请重传");
      }
    })
  },

 

标签: js 微信小程序
最后更新:2021年4月2日

博主

全栈工程师,侧重项目技术解决方案规划和开发

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

分类
  • .NET (65)
  • docker (3)
  • linux (12)
  • python (20)
  • web (14)
  • 小程序 (4)
  • 数据库 (2)
  • 未分类 (4)
  • 杂七杂八 (10)
标签聚合
DevExpress winform python nginx js centos linux C#
最新 热点 随机
最新 热点 随机
.NET开发手册标准参考 招募兼职前端开发 Centos安装dotnet6环境 VS上切换分支,vs编译运行出现bug,A fatal error was encountered彻底解决方案 用C#封装一个线程安全的缓存器,达到目标定时定量更新入库 C#通过特性的方式去校验指定数据是否为空
C#读写锁的一种方便写法 机器学习目标检测之印章检测和分类 微信小程序 音频播放功能createInnerAudioContext python版本的图片比较相似度 WinForm使用简单的方法实现完美遮罩的效果 剪切pdf分割程图片和pdf

COPYRIGHT © 2021 陈程的技术博客. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS