帮我写一段element-ui上传语音文件的前端代码
时间: 2023-06-09 11:08:20 浏览: 74
好的,以下是使用 element-ui 实现上传语音文件的前端代码:
```html
<template>
<el-upload
class="upload-demo"
ref="upload"
:action="uploadUrl"
:headers="headers"
:before-upload="beforeUpload"
:on-success="onSuccess"
:on-error="onError"
:on-exceed="handleExceed"
:file-list="fileList"
:auto-upload="false"
:multiple="false"
accept="audio/*">
<el-button size="small" type="primary">点击上传语音文件</el-button>
<div slot="tip" class="el-upload__tip">只能上传音频格式的文件</div>
</el-upload>
</template>
<script>
export default {
data() {
return {
fileList: [], // 已上传的文件列表
uploadUrl: '/upload/audio', // 上传地址
headers: { // 上传请求头
Authorization: 'Bearer ' + localStorage.getItem('token')
}
}
},
methods: {
// 上传之前的钩子
beforeUpload(file) {
const isLt10M = file.size / 1024 / 1024 < 10
if (!isLt10M) {
this.$message.error('上传文件大小不能超过 10MB!')
}
return isLt10M
},
// 上传成功的回调
onSuccess(response, file, fileList) {
this.fileList = fileList
this.$message.success('上传成功')
},
// 上传失败的回调
onError(error, file, fileList) {
this.fileList = fileList
this.$message.error('上传失败')
},
// 上传数量超过限制的回调
handleExceed(files, fileList) {
this.$message.warning('只能上传一个文件')
}
}
}
</script>
```
请注意,这只是一个示例,具体的实现方式取决于你的具体需求。如果还有其他问题,欢迎随时向我提问。