微信小程序> 微信小程序上传图片音频视频

微信小程序上传图片音频视频

浏览量:2073 时间: 来源:utf--8

wxml:

button  bindtap='imageUpload'上传图片/buttonbutton  bindtouchstart="startRecode" bindtouchend="endRecode" class="cxbtn"上传音频/buttonbutton  bindtap='videoUpload'上传视频/button

js:

imageUpload(){    var that = this;//选择图片    wx.chooseImage({      count: 1,      sizeType: ['original', 'compressed'],      sourceType: ['album', 'camera'],      success(res) {        // tempFilePath可以作为img标签的src属性显示图片        const tempFilePaths = res.tempFilePaths        that.upImgs(tempFilePaths[0],0);      }    })  },//上传图片  upImgs: function (imgurl, index) {    var that = this;    wx.uploadFile({      url: api.image_upload,//      filePath: imgurl,      name: 'file',      header: {        'content-type': 'multipart/form-data'      },      formData: null,      success: function (res) {        var data = JSON.parse(res.data);        var data2 = JSON.parse(data.data);        that.setData({          picPath: data2.path        })        wx.showToast({          title: '图片上传成功',          icon: 'success',          duration: 2000        })      }    })  },   //选择音频  startRecode: function () {    var that = this;    console.log("start");    wx.startRecord({      success: function (res) {        console.log(res);        var tempFilePath = res.tempFilePath;        that.setData({          recodePath: tempFilePath,           isRecode: true         });      },      fail: function (res) {        console.log("fail");        console.log(res);        //录音失败      }    });  },  endRecode: function () { //结束录音     var that = this;    console.log("end");    wx.stopRecord();    that.setData({       isRecode: false    });    // wx.showToast();    setTimeout(function () {      console.log(that.data.recodePath);      wx.uploadFile({        url: api.audio_upload,//上传接口        filePath: that.data.recodePath,        name: 'file',        header: {          'content-type': 'multipart/form-data'        },        success: function (res) {          var str = res.data;          var data = JSON.parse(str);          if (data.states == 1) {            var cEditData = s.data.editData;            cEditData.recodeIdentity = data.identitys;            that.setData({ editData: cEditData });          }          else {            wx.showModal({              title: '提示',              content: '上传成功',              showCancel: false,              success: function (res) {              }            });          }          wx.hideToast();        },        fail: function (res) {          console.log(res);          wx.showModal({            title: '提示',            content: "网络请求失败,请确保网络是否正常",            showCancel: false,            success: function (res) {            }          });          wx.hideToast();        }      });    }, 1000)  },//选择视频videoUpload : function () {    var that = this    wx.chooseVideo({      sourceType: ['album', 'camera'],      maxDuration: 60,      camera: 'back',      success: function (res) {        that.setData({          video: res.tempFilePath,          size: (res.size / (1024 * 1024)).toFixed(2)        })        // 调用上传接口      }    })  },

 

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎