1.传统方式
<input type="file" ref="fileInput" @change="getFile" />
<script>
getFile(event) {
const reader = new FileReader();
reader.readAsText(event.target.files[0], "UTF-8");
reader.onload = (e) => {
const fileString = e.target.result;
this.uploadForm.key = fileString;
};
},
</script>
2.el-upload上传文件发送文件到后端
<el-upload
class="upload-demo"
action="https://"
:on-change="handleChange"
:show-file-list="false"
:multiple="false"
:auto-upload="false"
>
<el-button
size="small"
type="primary"
icon="el-icon-upload2"
></el-button>
</el-upload>
<script>
export function certLdap(data) {
return request({
url: "/v1/cert",
method: "post",
data,
contentType: "multipart/form-data"
});
}
handleChange(file) {
let formData = new FormData();
formData.append("cert", file.raw);
certLdap(formData).then((res) => {
this.postForm.server_certificate = res.data.cert;
});
},
</script>