// 上传视频
videoUpload() {
var _this = this;
var timestamp = Date.parse(new Date());
var date = new Date(timestamp);
// console.log("==现在时间==")
// console.log(date)
var Y = date.getFullYear(); //年
var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1); //月
var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); //日
var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours(); //时
var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(); //分
var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(); //秒
// console.log(Y, M, D, h, m, s);
uni.chooseVideo({
sourceType: ['camera', 'album'],
success: function(res) {
console.log(res);
var tempFilePath = res.tempFilePath;
var tempName = tempFilePath.slice(11, 23);
// console.log(tempName);
var tempFormat = tempFilePath.substring(tempFilePath.lastIndexOf(".") + 1);
// console.log(tempFormat);
// 参数格式:2022_03_10_uni/2022-03-10_11:23:42_ab9.mp4
var filePath = Y + '_' + M + '_' + D + '_uni/' + Y + '-' + M + '-' + D + '_' + h + ':' + m + ':' + s + '_' + tempName + '.' + tempFormat;
console.log(filePath)
// 普通上传
uni.uploadFile({
url: 'https://up-z2.qiniup.com', //华南区
filePath: tempFilePath,
name: 'file',
formData: {
'key': filePath,
'token': _this.qiniuConfig.uptoken, //后台返回七牛云上传token
},
success: (res) => {
console.log('==上传结果==')
// console.log(res);
if (res.statusCode == 200) {
let result = JSON.parse(res.data);
console.log(result)
_this.videoSrc = _this.qiniuConfig.domain + '/' + result.key;
} else {
uni.showToast({
title: '上传失败',
icon: 'none',
duration: 2000
});
}
}
});
}
});
},
七牛云文件上传接口,文件向匹配的接口中传输,存储区域对应 HTTPS 地址。
存储区域 | 区域代码 | HTTPS地址 |
华东 | ECN | https://up.qiniup.com |
华北 | NCN | https://up-z1.qiniup.com |
华南 | SCN | https://up-z2.qiniup.com |
北美 | NA | https://up-na0.qiniup.com |
新加坡 | ASG | https://up-as0.qiniup.com |
获取七牛云token示例
参考文献:基于七牛云 API 开发的微信小程序