前提
import TcVod from 'vod-js-sdk-v6'
前台代码
<el-dialog title="课程视频上传" :visible.sync="box" width="50%">
<div class="upload_video" id="typeContent">
<el-upload class="upload-demo" ref="upload" action="#" :limit="1" :on-remove="handleRemove"
:on-exceed="handleExceed"
:on-change="handleChange" :auto-upload="false">
<el-button slot="trigger" size="small" type="primary">选取素材(图片/视频)</el-button>
<el-button style="margin-left: 10px; margin-bottom: 10px;" size="small" type="success" @click="submitUpload">
点击上传
</el-button>
<el-progress class="progress" :text-inside="true" :stroke-width="18" :percentage="progress"
status="exception"></el-progress>
<div slot="tip" class="el-upload__tip">提示:如果为视频类型,请上传100M以内同内容标清视频</div>
</el-upload>
</div>
</el-dialog>
js代码
data() {
return {
item:null,
box:false,
fileList: [],
videoURL: '',
progress: 0,
imgBase: '',
fileId: '',
方法
uploadVides(item){
this.box=true;
this.item=item;
},
handleExceed(files, fileList) {
this.$message.warning(`当前限制选择 1 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
},
handleRemove() {
this.videoName = '';
},
handleChange(file, fileList) {
this.fileList = fileList
},
submitUpload() {
if (this.videoName != null && this.videoName !== '') {
return this.$message.warning('视频已经上传完成,不可再次上传');
} else if (this.fileList.length < 1) {
return this.$message.warning('请先选取视频,再进行上传');
} else {
this.uploadVideo()
}
},
uploadVideo() {
this.videoName = this.fileList[0].raw.name;
const getSignature = async () => {
return await this.getVodSignature()
}
var sigbn=this.getVodSignature();
const tcVod = new TcVod({
getSignature: getSignature
})
const mediaFile = this.fileList[0].raw
const uploader = tcVod.upload({
mediaFile: mediaFile
})
uploader.on('media_progress', info => {
this.progress = parseInt(info.percent * 100)
})
uploader.done().then(doneResult => {
this.fileId = doneResult.fileId
this.item.filePath = doneResult.video.url
this.item.name=this.videoName;
this.box=false;
})
},
getVodSignature() {
return courseApi.getUploadSignature({}).then(re => {
return re;
})
},