小程序视频上传
/**
* 拍摄或选择视频并上传服务器
*/
chooseVideo: function () {
let that = this
//1.拍摄视频或从手机相册中选择视频
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.data.duration = duration
if (parseFloat(size) > 100) {
that.setData({
clickFlag: true,
duration: ''
})
let beyondSize = parseFloat(size) - 100
wx.showToast({
title: '上传的视频大小超限,超出' + beyondSize + 'MB,请重新上传',
//image: '',//自定义图标的本地路径,image的优先级高于icon
icon: 'none'
})
} else {
//2.本地视频资源上传到服务器
that.uploadFile(tempFilePath)
}
},
fail: function () {
// fail
},
complete: function () {
// complete
}
})
},
/**
* 将本地资源上传到服务器
*
*/
uploadFile: function (tempFilePath) {
let that = this
wx.showLoading({
title: '上传进度:0%',
mask: true //是否显示透明蒙层,防止触摸穿透
})
const uploadTask = wx.uploadFile({
url: app.globalData.baseUrl + '?m=Index&a=upload_save', //开发者服务器地址
filePath: tempFilePath, //要上传文件资源的路径(本地路径)
name: 'file', //文件对应key,开发者在服务端可以通过这个 key 获取文件的二进制内容
// header: {}, // 设置请求的 header
formData: {
'video': 'video',
}, // HTTP 请求中其他额外的 form data
success: function (res) {
// console.log("uploadFile", res)
// success
let data = JSON.parse(res.data)
wx.hideLoading()
if (data.code == 200) {
that.setData({
'form.case_video': data.url,
})
wx.showToast({
title: '上传成功',
icon: 'success'
})
} else {
wx.showToast({
title: '上传失败',
icon: 'none'
})
}
},
fail: function () {
// fail
wx.hideLoading()
wx.showToast({
title: '上传失败',
icon: 'none'
})
}
})
//监听上传进度变化事件
uploadTask.onProgressUpdate((res) => {
wx.showLoading({
title: '上传进度:' + res.progress + '%',
mask: true //是否显示透明蒙层,防止触摸穿透
})
// console.log("上传进度", res.progress)
// console.log("已经上传的数据长度,单位 Bytes:", res.totalBytesSent)
// console.log("预期需要上传的数据总长度,单位 Bytes:", res.totalBytesExpectedToSend)
})
},
免责申明:
1. 本站所有教程、文章或资源分享目的仅供大家学习和交流!
2. 如有无法查看或链接失效,麻烦请报告联系管理员处理!
3. 本站无法保证资源或其时效性,恕不接受任何提问。
4. 在本站下载的源码严禁杜绝任何形式的正式商业用途,请去程序官方购买。 所有资料均来自于网络,版权归原创者所有!本站不提供任何保证,并不承担任何法律责任,如果对您的版权或者利益造成损害,请提供相应的资质证明,我们将于3个工作日内予以删除。