小程序的图片以及语音上传

微信小程序作为一种比较新的前端技术,继续保持了数据驱动的特点,小程序依附微信这个框架平台,提供了很多比较实用的开放接口,录音和上传照片就在其中,wxml代码如下:
<view class='importObj'>
   <image src='../../images/home/icon-pic.png' bindtap='choosePic'></image>
   <image src='../../images/home/icon-sound.png' bindtouchstart="touchdown" bindtouchend="touchup"></image>
</view> 

 对应的js如下:

choosePic:function(){
    if(this.data.getImg.length>=6){
      Util.tip('最多选择六张图片')
      return
    }
    var that = this;
    var imgcount = 6 - this.data.getImg.length
    wx.chooseImage({
      count: imgcount,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        if (tempFilePaths.length>0){
          wx.showLoading({
            title: '上传中',
            mask: true
          })
          var promises = [];
          for(var i =0 ;i<tempFilePaths.length;i++){
            promises.push(that.uploadImg(that, tempFilePaths[i]))
          }
          Promise.all(promises).then(function(){
            wx.hideLoading()
          })
        }
      }
    })
  },
  uploadImg:function(that,path){
    return new Promise(function (resolve, reject) {
      Yyc.uploadFile({
        url: ctx + '/api/mini/homeschool/sendMessageAttachment',
        filePath: path,
        name: 'fileupload',
        formData: {
          'type': 1
        },
        success(res) {
          if (res.data.ret == 0) {
            const data = res.data
            var lists = that.data.getImg;
            var newData = { img: data.path };
            lists.push(newData);
            that.setData(that.data);
            resolve()
          } else {
            Util.tip('上传失败')
            reject();
          }
        }
      })
    })
  }

 上传照片和H5的方式一样,调取原生的js方法,在微信小程序里,提供了更简洁的使用参数,比如控制单次上传数目,以及比对选取文件类型,语音录入和上传,我们使用一张上传中的gif图,在模拟长按事件中展示,语音以及图片不需要接口,也可以实现点击播放和展示之类的,上传代码为真实项目,仅供参考,语音成功录入以后,会返回时长之类的参数,供展示来用,代码如下:

confirmSend:function(){
var that = this
if (that.data.voiceShow){
wx.showLoading({
title: '上传语音中',
})
Yyc.uploadFile({
url: ctx + '/api/mini/homeschool/sendMessageAttachment',
filePath: that.data.tempFilePath,
name: 'fileupload',
formData: {
'type': 1
},
success(res) {
wx.hideLoading()
if (res.data.ret == 0) {
const data = res.data
that.setData({ voiceUrl: res.data.path })
that.realSend(that)
} else {
Util.tip('上传失败')
}

}
})
 

}else{
that.realSend(that)
}

},
touchdown: function () {
  console.log("手指按下了...")
  var _this = this;
  _this.setData({
  isSpeaking: true,
  beginTime: new Date().getTime()
})
const recordManager = wx.getRecorderManager();
recordManager.onStop(function(res){
var tempFilePath = res.tempFilePath;
if (res.duration<3000){
Util.tip('录音时间太短哟')
return
}
console.log("tempFilePath: " + tempFilePath);
_this.setData({
tempFilePath: tempFilePath,
lastTime: parseInt(res.duration/1000),
voiceShow: true
})
})
recordManager.start(
{
duration:300000,
format:'mp3'
}
);
},
//手指抬起
touchup: function () {
console.log("手指抬起了...")
this.setData({
isSpeaking: false,
})
const recordManager = wx.getRecorderManager();
recordManager.stop()
},
//点击播放录音
gotoPlay: function (e) {
var filePath = e.currentTarget.dataset.key;
const InnerAudioContext = wx.createInnerAudioContext();
InnerAudioContext.src = filePath;
InnerAudioContext.play()
},

 对应的wxml如下:

<view wx:if="{{isSpeaking}}" class="microphone">
  <view>
      <image class="sound-style" src="../../images/home/voice.gif"></image>
      <text>按住说话</text>
  </view>
</view>

 语音的展示、删除以及播放wxml如下:

<view wx:if="{{voiceShow}}" class="common-list">
      <view class='audioContet' data-key="{{tempFilePath}}" bindtap="gotoPlay">
        <label>语音</label><label>{{lastTime}}"</label>
      </view>
      <image src='../../images/home/icon-closed.png' bindtap="delVideo"></image>
  </view>

 

转载于:https://www.cnblogs.com/sakura-lifangfang/p/11412739.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值