小程序上传视频
.js文件
Page({data: { webServer: 'https://xxxx.xxx.com', src: '', webviewshow: 'hide', webviewurl: '', gid: "", uid: '', duration: '', height: '', size: '', width: '', ThumbPath: false, videoshow: 'hide'}, onLoad: function (options) { this.setData({ gid: options.gid, uid: options.uid }) }, chooseVideo: function () { var that = this wx.chooseVideo({ success: function (res) { that.setData({ src: res.tempFilePath, }) } }) }, getLocalVideo: function () { var that = this; var session_key = wx.getStorageSync('session_key'); wx.chooseVideo({ maxDuration: 10, success: function (res1) { that.setData({ src: res1.tempFilePath, duration: res1.duration, height: res1.height, size: res1.size, width: res1.width, videoshow: 'thumb' }) wx.showToast({ title: '选择成功', icon: 'succes', duration: 2000, mask: true }) }, fail: function () { console.error("获取本地视频时出错"); } }) }, uploadvideo: function () { var that = this; wx.showLoading({ title: '上传中', }) var src = this.data.src; if (src) { that.setData({ ThumbPath: true }) } wx.uploadFile({ url: that.data.webServer + 'xxxxxx', formData: { 'uid': that.data.uid, 'gid': that.data.gid, 'duration': that.data.duration, 'height': that.data.height, 'size': that.data.size, 'width': that.data.width }, method: 'POST', filePath: src, header: { 'content-type': 'multipart/form-data' }, name: 'files', success: function (e) { wx.hideLoading(); if (typeof e.data != 'object') { e.data = e.data.replace(/ufeff/g, ""); var data = JSON.parse(e.data); if (data.status == 1) { wx.showToast({ title: '上传成功', icon: 'succes', duration: 1000, mask: true }) setTimeout(function () { that.backHtml(); }, 1000) } else if (data.status == 2) { wx.showToast({ title: '文件过大', icon: 'succes', duration: 1000, mask: true }) } } }, fail: function () { wx.showToast({ title: '上传失败', icon: 'succes', duration: 1000, mask: true }) } }) }, backHtml: function () { var that = this; wx.reLaunch({ url: '/pages/index/index?gid=' + this.data.gid }) },onReady: function () {},onShow: function () {},onHide: function () {},onUnload: function () {},onPullDownRefresh: function () {},onReachBottom: function () {},onShareAppMessage: function () {}})
.wxml
<view class='contbox'> <view class='thumbbox'> <video class="{{videoshow}}" src="{{src}}"></video> </view> <!-- 提交 --> <button bindtap='getLocalVideo' class="btn2" type="primary" form-type='submit'>选择视频</button> <button type="default" class="btn2" bindtap="chooseVideo" bindtap='uploadvideo' type="warn" form-type='reset'>上传视频</button></view>
.wxss
.thumbbox { display: flex; justify-content: center; align-items: center;}
