<el-upload
:on-change="handleChansge"
:on-remove="handleRemove"
:on-exceed="handleExceed"
:before-remove="beforeRemove"
:http-request="upload"
:auto-upload="false"
:limit="1"
:file-list="fileList"
action="#"
accept=".jpg,.png,.pdf,.doc,.docx,.JPG,.PNG,.PDF,.DOC,.DOCX"
multiple>
<el-button size="small" type="primary">上传简历</el-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png/pdf/doc/docx文件,且不超过5MB</div>
</el-upload>
data() {
return {
fileList: [],
}
}
handleChansge(file) {
if (!/\.(jpg|png|pdf|doc|docx)$/i.test(file.name)) {
this.$message.error('只能上传jpg/png/pdf/doc/docx文件!')
this.fileList = []
return false
}
const isLt5M = file.size / 1024 / 1024 <= 5
if (!isLt5M) {
this.$message.error('上传文件大小不能超过 5MB!')
this.fileList = []
return false
}
this.file = file.raw
this.upload()
},
handleRemove() {
this.resume_url = ''
},
handleExceed() {
this.$message.warning(`当前只限制选择 1 个文件`)
},
beforeRemove(file, fileList) {
return this.$confirm(`确定移除 ${file.name}?`)
},
async upload() {
const formData = new FormData()
formData.append('file', this.file)
resumeUpload(formData).then(data => {
const { errorCode, data: { path }} = data.data
if (errorCode === 0) {
this.resume_url = path
this.$message.success(`上传成功`)
} else {
this.filelist = []
this.file = ''
this.$message.error(`上传失败`)
}
}).catch(err => {
console.log(err)
this.filelist = []
this.file = ''
this.$message.error(`上传失败`)
})
},
import * as fs from 'fs';
import * as path from 'path';
import * as fse from 'fs-extra';
import * as uuidv4 from 'uuid/v4';
async upload() {
const { ctx } = this;
try {
let file = ctx.request.files[0]
const DirPath = path.join(this.config.static.dir, ZHIPING_INTERVIEW_DIR);
await fse.ensureDir(DirPath);
const prefixPath = `/public/${ZHIPING_INTERVIEW_DIR}`;
const newFilename = uuidv4() + path.extname(file.filename);
let fileObiect = fs.readFileSync(file.filepath)
fs.writeFileSync(path.join(DirPath,newFilename), fileObiect)
ctx.helper.success({
path: `${prefixPath}${newFilename}`
});
} catch(err) {
ctx.helper.error(500, '上传失败!');
}
}
async delResume(prefixPathName) {
const fileName = prefixPathName.replace(/\/public\/zhiping_interview\//g,'')
const DirPath = path.join(this.config.static.dir, ZHIPING_INTERVIEW_DIR);
console.log(`${DirPath}${fileName}`)
return fs.unlinkSync(`${DirPath}${fileName}`)
}