示例如下
template
<el-form-item label="选择文件">
<el-upload
class="upload-demo"
ref="upload"
:limit="1"
:file-list="fileList"
:on-change="handleChange"
:action="uploadUrl"
:show-file-list="true"
:on-success="onSuccess"
:on-error="onError"
:auto-upload="false"
>
<el-button type="primary" slot="trigger">选取文件</el-button>
<el-button
type="primary"
@click="handleSubmit"
style="margin-left: 10px;"
>上传文件</el-button
>
</el-upload>
</el-form-item>
js
<script>
export default {
data() {
return {
fileList: [],
uploadUrl: "",
uploadfileForm: {
fileList: null,
},
}
},
methods:{
onSuccess(res) {
console.log(res);
if (+res.code === 0) {
this.queryFileUpload.data.fileUrl = res.data;
} else {
this.$message.error("上传失败");
}
},
onError(res) {
console.log(res);
this.$message.error("创建失败");
},
handleChange(file, fileList) {
if (fileList.length > 0) {
this.uploadfileForm.fileList = [fileList[fileList.length - 1]];
}
},
handleSubmit() {
this.uploadUrl = `${rootUrl}commApp/file`;
this.$nextTick(() => {
this.$refs.upload.submit();
});
},
}
}
</script>