<el-upload
style="display: inline; margin-left: 20px"
class="upload-demo"
ref="upload"
:action="upLoadUrl"
accept=".docx"
:show-file-list="false"
:headers="uploadheaders"
:before-upload="beforeUpload"
:on-success="handle_success"
:data="upLoadText"
>
<!-- <el-button type="danger" @click="viewFile" icon="el-icon-view"
>预览</el-button
> -->
</el-upload>
<el-button slot="trigger" type="primary" @click="upLoadFile"
>上传</el-button
>
data() {
return {
uploadheaders: {
Authorization: "",
},
upLoadText: {},
addForm: {
file: null,
fileName: "",
fileData: null,
},
}
},
computed: {
upLoadUrl() {
return process.env.VUE_APP_BASE_API + "/portal-file/upload";
},
},
methods: {
beforeUpload(file) {
this.uploadheaders.Authorization = "Bearer " + getToken();
this.addForm.file = file;
this.addForm.fileName = file.name;
const extension = file.name.split(".").slice(-1) == "docx";
if (!extension) {
this.$message.warning("上传模板只能是word格式!");
return;
}
let reader = new FileReader();
reader.readAsDataURL(file);
let that = this;
reader.onload = function () {
that.addForm.fileData = reader.result;
};
return true;
},
handle_success(res) {
console.log("上传成功:", res);
this.query.sourcePath = res.data.url;
this.query.sourceName = res.data.name;
console.log("query:", this.query);
upload(this.query).then((res) => {
this.$message({
message: "上传成功",
type: "success",
});
this.query.id = "";
});
},
upLoadFile() {
if (this.query.id) {
this.toSelectFile();
this.search();
} else {
this.$message({
message: "请选择要上传文件的模板",
type: "warning",
});
}
},
toSelectFile() {
this.upLoadText = {
creator: window.sessionStorage.getItem("accountName"),
fileName: this.addForm.fileName,
requirement: this.addForm.selectedRequirement,
};
this.$refs["upload"].$refs["upload-inner"].handleClick();
console.log(this.addForm);
},
}