第一种方法:将file转码后 使用json传过去
DOM:
<el-upload
ref="upload"
action="/"
:before-upload="beforeUpload"
:default-file-list="fileList"
:limit="1"
>
<el-button slot="trigger" size="small" type="primary">上传文件</el-button>
</el-upload>
<div slot="tip" class="el-upload__tip">只能上传 *** 文件</div>
JS:
beforeUpload(file) {
let fileName = file.name;
this.fileList = [file];
this.read(file)
return false;
},
read(f) {
let rd = new FileReader();
rd.onload = (e) => {
this.form.file = rd.reading({encode: "UTF-8"})
};
rd.readAsBinaryString(f);
},
最终效果:

-----------------------------------------------分割线--------------------------------------------------------------
第二种方法:使用formData
DOM:
<el-upload
ref="upload"
action="/"
:on-change="handleChange"
:file-list="fileList"
:limit="1"
>
<el-button slot="trigger" size="small" type="primary">上传文件</el-button>
</el-upload>
<div slot="tip" class="el-upload__tip">只能上传 *** 文件</div>
JS:
handleChange(file) {
this.file = file.raw
},
submitForm(){
let form = new FormData();
form.append('file', this.file )
form.append('name', this.form.name)
form.append('age', this.form.age)
this.axios({
method: "post",
url: url,
data: form,
}, {
headers: {
'Content-Type': 'multipart/form-data'
}
}).then(res=>{
})
},
最终效果:
