频道栏目
首页 > 资讯 > HTML/CSS > 正文

微信小程序录音后上传文件

17-08-08        来源:[db:作者]  
收藏   我要投稿

微信小程序录音后上传文件

  startRecode:function(){
  var s = this;
  console.log("start");
  wx.startRecord({
      success: function (res) {
          console.log(res);
          var tempFilePath = res.tempFilePath;
          s.setData({ recodePath: tempFilePath, isRecode:true});
      },
      fail: function (res) {
          console.log("fail");
          console.log(res);
          //录音失败
      }
  });
  },
  endRecode:function(){//结束录音 
  var s = this;
  console.log("end");
  wx.stopRecord();
  s.setData({ isRecode: false });

   
  wx.showToast();
  setTimeout(function () {
      var urls = app.globalData.urls + "/Web/UpVoice";
      console.log(s.data.recodePath);
      wx.uploadFile({
          url: urls,
          filePath: s.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;
                  s.setData({ editData: cEditData });
              }
              else {
                  wx.showModal({
                      title: '提示',
                      content: data.message,
                      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)
 
  }

页面代码:

<button type="primary" bindtouchstart="startRecode" bindtouchend="endRecode" class="cxbtn">按住录音(可选)</button>

总结:录音后要延迟加载,微信录音后生成文件需要一段时间,如果不延迟加载可能无法获取到文件名 提示:uploadFile:localid is empty

相关TAG标签
上一篇:使用JavaScript实现表单校验
下一篇:通过ajax得到数据,显示http://localhost:8081/项目名/[object%20Object]404(NotFound)错误。
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站