微信小程序实现图片上传功能

微信小程序实现图片上传和视频上传功能

服务器:阿里云
后端:PHP,提供上传接口

用到相关API:
wx.showActionSheet:显示操作菜单
在这里插入图片描述

wx.chooseImage:从本地相册选择图片或使用相机拍照,该api后期官方不维护,可使用 wx.chooseMedia api
在这里插入图片描述
wx.chooseVideo:拍摄视频或从手机相册中选视频
在这里插入图片描述

wx.chooseMedia:拍摄或从手机相册中选择图片或视频,官方提示:此接口不再更新,建议使用 wx.chooseMedia
在这里插入图片描述

选择需要上传的文件:

//可以选择视频或者图片
 uploadFiles(){
    var _this = this;
      wx.showActionSheet({
          itemList: ['选择图片', '选择视频'],
          success: function (res) {
              let xindex = res.tapIndex;
              if (xindex == 0){
                // 选择图片
                  _this.chooseImage()
              } else if (xindex == 1){
                // 选择视频
                _this.chooseVideo()
              }
          },
          fail: function (res) {
              console.log(res.errMsg)
          }
      })
  },

选择图片或拍照

//  选择图片类型
  chooseImage(){
    wx.chooseImage({
      count: 1,//上传数量
      sizeType: ['original', 'compressed'],//图片:原图和压缩
      sourceType: ['album', 'camera'], // 选择图片的来源
      success: (res) => {
        let arr = res.tempFiles;
        var imgInfo = {};
        arr.map(function(v,i){
            v['progress'] = 0;
            imgInfo=v
        })
        _this.upImgs(imgInfo,'image')
      }
    })
  },

选择视频或拍摄

chooseVideo(){
    let _this=this
    wx.chooseVideo({
      count: 1,//上传数量
      maxDuration:"30",//拍摄时长
      sizeType: ['original', 'compressed'],//图片:原图和压缩
      sourceType: ['album', 'camera'], // 选择图片的来源
      // sourceType:"album",
      compressed:true,
      camera: 'back',
      success: (res) => {
        let videoInfo = {};
        videoInfo['tempFilePath'] = res.tempFilePath;//路径
        videoInfo['size'] = res.size;// 大小
        videoInfo['height'] = res.height;
        videoInfo['width'] = res.width;
        videoInfo['thumbTempFilePath'] = res.thumbTempFilePath;//封面
        videoInfo['progress'] = 0;
        _this.upImgs(videoInfo,'video')
       
      }
    })
  }, 

上传服务器

 upImgs(dataInfo, type) {
     let that=this
     wx.showLoading({
       title: '上传中',
       mask:true,
     })
     wx.uploadFile({
       url: '上传的网络地址',
       filePath: type=='video'?dataInfo.tempFilePath:dataInfo.path,
       formData:null,
       name: 'file',
       header: {
         'content-type': 'multipart/form-data'
       },
       success:(res=>{
           wx.hideLoading()
          //判断上传的是图片还是视频
            if(type=="video"){
              that.setData({
              	console.log('视频地址:'+data.data.src)
              	console.log('视频封面:'+data.data.src+'?spm=qipa250&x-oss-process=video/snapshot,t_1000,f_jpg,w_800,h_400,m_fast')
            }else{
             console.log('图片地址:'+ data.data.src)
            }
       })
     })
   },
  • 6
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值