upload上传及回显
<el-upload ref="uploadRef" :action="uploadAction" :accept="uploadFileType.join(',')" :before-upload="handleUploadRule" :on-change="handleUploadChange" :on-success="handleUploadSuccess" :on-error="handleUploadError" :on-preview="handleUploadPreview" :file-list="fileList" :multiple="false" drag>
<i class="el-icon-upload"></i>
<div class="el-upload__text">
将文件拖到此处,或<em>点击上传</em>
</div>
<div class="el-upload__tip" slot="tip">只能上传pdf文件</div>
</el-upload>
import { baseUrl } from "../../../public/ipConfig";
data(){
return{
uploadAction: baseUrl.base + "/preplan/info/upload",
uploadFileType: [".pdf"],
fileList: [],
}
}
addList () {
this.Sign = "add";
this.title = "新建";
this.disabled = false;
this.dialogVisible = true;
this.form = {};
this.fileList = [];
},
editList (row) {
this.Sign = "edit";
this.title = "编辑";
this.disabled = false;
this.dialogVisible = true;
this.form = JSON.parse(JSON.stringify(row));
this.getResourceData(row.id).then(() => {
console.log(this.topResourceData, "this.topResourceData");
this.handleShowData(this.topResourceData);
});
console.log(row, "row");
if (row.file == "" && row.fileName == "") {
this.fileList = [];
} else {
this.fileList = [
{
name: row.fileName,
url: row.fileUrl,
},
];
}
},
handleUploadRule (file) {
let type = file.name.match(/.[^.]+$/)[0].toLowerCase();
if (!this.uploadFileType.includes(type)) {
this.$message.error(
"格式错误,仅支持 " + this.uploadFileType.join(" | ") + " !"
);
return false;
}
return true;
},
handleUploadSuccess (res) {
if (res.code == 200) {
this.file = res.data.relativePath;
this.fileName = res.data.fileName;
console.log(this.file, "this.file");
this.$message.success("文件上传成功!");
} else {
this.handleUploadError();
}
},
handleUploadError () {
this.$message.error("文件上传失败!");
},
handleUploadChange (file, fileList) {
if (fileList.length > 1) {
fileList.splice(0, 1);
}
this.fileList = fileList;
},
handleUploadPreview (file) {
console.log(file, '===上传列表预览===');
let url = '';
if (file.url) {
url = file.url
} else if (file.response && file.response.data) {
url = file.response.data.relativePath
}
this.previewFile = url;
this.previewVisible = true;
},