微信小程序分别上传图片和视频

微信小程序分别上传图片和视频

dksjxc: function () {
const that = this;
// 最多上传图片数量
if (that.data.imgList.length < that.data.maxImg) {
wx.chooseImage({
count: that.data.maxImg - that.data.imgList.length, // 最多可以选择的图片张数(最大数量-当前已上传数量)
sizeType: [‘original’, ‘compressed’], // 可以指定是原图还是压缩图,默认二者都有
sourceType: [‘album’, ‘camera’], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
console.log(res)
for (let i = 0; i < res.tempFilePaths.length; i++) {
that.data.imgList.push(res.tempFilePaths[i]);
wx.uploadFile({
// url: ‘自己的接口’,
method: “post”,
filePath: that.data.imgList[i],
name: ‘file’,
header: {
“Content-Type”: “multipart/form-data”,
‘accept’: ‘application/json’,
// ‘Authorization’: ‘Bearer …’ //若有token,此处换上你的token,没有的话省略
},
formData: {
‘user’: ‘test’,
filePath: that.filePath,
},
success(res) {
const data = res.data
console.log(res.data);
that.data.img = JSON.parse(res.data).data;
console.log(that.data.img)
}
})
}

      // 显示图片(同步渲染到页面)
      console.log(that.data.imgList)
      that.setData({
        imgList: that.data.imgList
      }) 
    }
  })
} else {
  wx.showToast({
    title: "最多上传" + that.data.maxImg + "张照片!"
  })
}

},
//视频上传
dksjsp:function(){
wx.chooseVideo({
sourceType: [‘album’, ‘camera’], // album 从相册选视频,camera 使用相机拍摄
// maxDuration: 60, // 拍摄视频最长拍摄时间,单位秒。最长支持60秒
camera: ‘back’,//默认拉起的是前置或者后置摄像头,默认back
compressed: true,//是否压缩所选择的视频文件
success: function (res) {
console.log(res)
let tempFilePath = res.tempFilePath//选择定视频的临时文件路径(本地路径)
// let duration = res.duration //选定视频的时间长度
// let size = parseFloat(res.size / 1024 / 1024).toFixed(1) //选定视频的数据量大小
// let height = res.height //返回选定视频的高度
// let width = res.width //返回选中视频的宽度
that.setData({
// clickFlag: true,
// duration: ‘’,
tempFilePath: res.tempFilePath,
})
wx.uploadFile({
// url: ‘自己的接口’,//服务器接口
method: ‘post’,
filePath: tempFilePath,
formData: {
filePath: that.filePath,
},
header: {
“Content-Type”: “multipart/form-data”,
‘accept’: ‘application/json’,
// ‘Authorization’: ‘Bearer …’ //若有token,此处换上你的token,没有的话省略
},
name: ‘file’,//服务器定义key字段名称
success: function (res) {
console.log(res)
that.data.video = JSON.parse(res.data).data;
console.log(‘视频上传成功’)
},
fail: function () {
console.log(‘视频上传失败’)
}
})
}
})
},
// 执行上传
conversionAddress: function () {
const that = this;
console.log(that.data.img)
console.log(that.video)
var scdata = {
“userId”: “4”,
“img”: that.data.img,
“text”: that.data.inputVale,
“video”:that.data.video,
};
var fajaxdata = JSON.stringify(scdata);
console.log(fajaxdata)
wx.request({
// url: “自己的接口”,
method: “post”,
data: fajaxdata,
header: {
“Accept”: “application/json”
},
success: function (res) {
console.log(res)
if (res.data.code == 0) {
wx.showModal({
title: “提示”,
content: “提交成功”,
success:function(res)
{
if(res.cancel)
{
//点击取消按钮

          }else if(res.confirm)
          {
            //点击确认按钮
            wx.switchTab({
              url: '../find/find',
              //跳转页面tabar以及跳转页面的刷新
              success() {
                var page = getCurrentPages().pop();
                if (page == undefined || page == null) return;
                page.onLoad();
              }
            })
          }
        }
      })
    } else {
      wx.showModal({
        title: "提示",
        content: "上传失败!"
      })
    }
  }
})

},

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值