vue+ant desgin pro 上传视频至阿里云
1. 在public文件目录的index.html中引入
<script src="http://mryt.kangdaedu.com/es6-promise.min.js"></script>
<script src="http://mryt.kangdaedu.com/aliyun-oss-sdk-5.3.1.min.js"></script>
<script src="http://mryt.kangdaedu.com/aliyun-upload-sdk-1.5.0.min.js"></script>
2. 前端编辑代码如下:
html:
<a-card type="inner" title="主备视频">
<a-row :gutter="16">
<a-col class="gutter-row" :span="2">
<a-button
type="primary"
style="margin:0 10px;float:right;"
@click="SeeAnalysisVoide"
size="large"
:disabled="this.teachVideoId==undefined||this.teachVideoId=='' ? true:false "
>预览</a-button>
</a-col>
<a-col class="gutter-row" :span="3">
<a href="#" class="file" icon="video-camera">
<input type="file" @change="getFile($event,'teachVideo')" accept=".mp4, .avi, .flv" />上传主备视频
</a>
</a-col>
<a-col class="gutter-row" :span="2">
<span
style="color:blue;width:200px;margin:10px 0px;display:block;"
>{{teachvideomessage}}</span>
</a-col>
<a-col class="gutter-row" :span="17">
<a-progress
:percent="teachVideoPercent"
status="active"
v-if="teachVideoUploading"
style="margin:8px 0;"
/>
</a-col>
</a-row>
</a-card>
data:
teachVideoUploading: false,
teachVideoPercent: 0,
teachvideomessage: "",
teachVideoId: "",
methods:{
getFile(event, type) {
if (event.target.files.length > 0) {
var file = event.target.files[0];
var imgName = file.name;
var idx = imgName.lastIndexOf(".");
if (idx != -1) {
var ext = imgName.substr(idx + 1);
ext = ext.toLowerCase();
if (ext != "mp4" && ext != "avi" && ext != "flv") {
this.$message.error(
"文件格式不符合要求,请上传mp4、avi、flv格式的视频文件!"
);
} else {
console.log(file);
var userData = '{"Vod":{}}';
if (this.uploader) {
this.uploader.stopUpload();
}
this.uploader = this.createUploader(type);
console.log(userData);
this.uploader.addFile(file, null, null, null, userData);
this.uploader.startUpload();
}
} else {
this.$message.error(
"请选择正确的视频文件,符合要求的文件格式为mp4、avi、flv!"
);
}
}
},
createUploader(type) {
let that = this;
let uploader = new AliyunUpload.Vod({
timeout: 60000,
userId: "1074034690116905",
partSize: 1048576,
parallel: 10,
retryCount: 10,
retryDuration: 2,
enableUploadProgress: true,
addFileSuccess: function(uploadInfo) {
console.log("添加文件成功, 等待上传...");
console.log("addFileSuccess: " + uploadInfo.file.name);
},
onUploadstarted: function(uploadInfo) {
if (uploadInfo.videoId) {
that.$api
.refreshUploadAuth({ videoId: that.teachVideoId })
.then(res => {
console.log("刷新视频上传凭证");
});
} else {
var title =
"【主备视频】第" + that.courseIndexText + "节 " + that.nameText;
var paramsData = {
coCourseDetailId: that.coCourseDetailId,
fileName: uploadInfo.file.name,
fileSize: uploadInfo.file.size,
title: title,
type: "teachVideo"
};
that.$api.getUploadAuth(paramsData).then(res => {
let uploadAuth = res.data.data.authMsg.uploadAuth;
let uploadAddress = res.data.data.authMsg.uploadAddress;
let videoId = res.data.data.authMsg.videoId;
that.teachVideoUploading = true;
uploader.setUploadAuthAndAddress(
uploadInfo,
uploadAuth,
uploadAddress,
videoId
);
that.teachVideoId = videoId;
});
}
},
onUploadSucceed: function(uploadInfo) {
that.$message.success("文件上传成功!");
},
onUploadFailed: function(uploadInfo, code, message) {
that.$message.success("文件上传失败,失败原因:" + message);
},
onUploadCanceled: function(uploadInfo, code, message) {
that.$message.success("文件已暂停上传!");
},
onUploadProgress: function(uploadInfo, totalSize, progress) {
that.teachVideoPercent = Math.ceil(progress * 100);
},
onUploadTokenExpired: function(uploadInfo) {
that.$api
.refreshUploadAuth({ videoId: that.teachVideoId })
.then(res => {
console.log("刷新视频上传凭证");
});
},
onUploadEnd: function(uploadInfo) {
that.teachVideoUploading = false;
if (that.teachVideoId !== undefined || that.teachVideoId !== "") {
that.teachvideomessage = "已上传主备视频!!!!";
return;
}
}
});
return uploader;
},
}