使用elelment上传组件,写一个简单的视频上传样式
时间: 2024-03-20 21:43:37 浏览: 117
好的,以下是一个简单的使用 ElementUI 的上传组件实现视频上传的样式:
```html
<template>
<div class="upload">
<el-upload
class="upload-video"
action="/api/upload"
:on-success="handleSuccess"
:on-error="handleError"
:before-upload="beforeUpload"
:show-file-list="false"
:auto-upload="false"
>
<el-button type="primary">选择视频文件</el-button>
<div slot="tip" class="upload-tip">只能上传 MP4 格式的视频文件,且不超过 100MB</div>
</el-upload>
<div class="upload-progress" v-if="showProgress">
<el-progress :percentage="progress"></el-progress>
<div class="upload-progress-text">{{ progress }}%</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
file: null,
showProgress: false,
progress: 0
};
},
methods: {
beforeUpload(file) {
const isMP4 = file.type === 'video/mp4';
const isLt100M = file.size / 1024 / 1024 < 100;
if (!isMP4) {
this.$message.error('只能上传 MP4 格式的视频文件');
}
if (!isLt100M) {
this.$message.error('视频文件大小不能超过 100MB');
}
return isMP4 && isLt100M;
},
handleSuccess(response, file) {
this.file = file;
this.showProgress = false;
this.$message.success('视频上传成功');
},
handleError(error) {
this.showProgress = false;
this.$message.error('视频上传失败');
},
uploadVideo() {
this.showProgress = true;
const formData = new FormData();
formData.append('video', this.file);
axios.post('/api/upload', formData, {
onUploadProgress: progressEvent => {
this.progress = Math.round((progressEvent.loaded * 100) / progressEvent.total);
}
}).then(response => {
this.handleSuccess(response.data, this.file);
}).catch(error => {
this.handleError(error);
});
}
}
};
</script>
<style>
.upload {
display: flex;
flex-direction: column;
align-items: center;
}
.upload-video {
margin-top: 20px;
width: 300px;
height: 180px;
border: 1px solid #e6e6e6;
border-radius: 4px;
text-align: center;
display: flex;
flex-direction: column;
justify-content: center;
font-size: 20px;
color: #999;
}
.upload-tip {
margin-top: 10px;
font-size: 14px;
color: #666;
}
.upload-progress {
margin-top: 20px;
width: 300px;
display: flex;
flex-direction: column;
align-items: center;
}
.upload-progress-text {
margin-top: 10px;
font-size: 14px;
color: #666;
}
</style>
```
这个样式使用了 ElementUI 的上传组件,并且添加了一些样式和逻辑,可以选择 MP4 格式的视频文件,且不超过 100MB,上传时会显示上传进度条。你可以根据自己的需求进行修改和完善。
阅读全文