html
<el-upload :file-list="blFileList" :class="{hide:upload.bl}" :http-request="((val)=>{return myUpload(val,1)})" :on-preview="handlePictureCardPreview" :on-remove="((file,fileList)=>{return handleRemove(file,fileList,1)})" :before-upload="handleBeforeUpload" :on-change="((file,fileList)=>{return handleChange(file,fileList,1)})" action="" list-type="picture-card">
<i class="el-icon-plus" />
</el-upload>
<el-dialog :visible.sync="bl_dialogVisible" :close-on-click-modal="false" :append-to-body="true">
<img :src="factoryFormData.bLicense" width="100%" alt="">
</el-dialog>
js
data() {
return {
blFileList: [],
limitNum: 1,
bl_dialogVisible: false,
upload: {
bl: false,
pl: false
},
factoryFormData: {
id: '',
name: '',
bUrls: '',
pUrls: ''
},
},
watch: {
'factoryFormData.bUrls'(newValue, oldValue) {
if (newValue) {
const path = this.baseUri + this.factoryFormData.bUrls
this.backfillPicture(1, path)
this.upload.bl = true
}
},
'factoryFormData.pUrls'(newValue, oldValue) {
if (newValue) {
const path = that.baseUri + that.factoryFormData.productionLicenseUrls
this.backfillPicture(2, path)
that.upload.pl = true
}
}
},
methods:{
handleBeforeUpload(file) {
const isIMAGE = file.type === 'image/jpg' || file.type === 'image/jpeg' || file.type === 'image/png'
const isLt1M = file.size / 1024 / 500 < 1
if (!isIMAGE) {
this.$message.error('上传文件只能是jpg或png图片格式!')
}
if (!isLt1M) {
this.$message.error('上传文件大小不能超过 500kb!')
}
return isIMAGE && isLt1M
},
handleRemove(file, fileList, type) {
if (type === 1) {
this.factoryFormData.bLicense = null
this.upload.bl = fileList.length >= this.limitNum
} else {
this.upload.pl = fileList.length >= this.limitNum
this.factoryFormData.pLicense = null
}
},
handlePictureCardPreview(file) {
this.factoryFormData.bLicense = file.url
this.bl_dialogVisible = true
},
myUpload(content, type) {
var form = new FormData()
form.append('file', content.file)
this.addFile(form, content.file, this.uploadActonUrl).then(res => {
if (res.id !== undefined && res.id !== null) {
content.onSuccess('文件上传成功!')
content.file.uid = res.id
const path = this.baseUri + res.path
this.backfillLicence(type, res.id, path, true)
} else {
content.onError('文件上传失败!')
}
}).catch(error => {
if (error.response) {
content.onError('文件上传失败,' + error.response.data)
} else if (error.request) {
content.onError('文件上传失败,服务器端无响应')
} else {
content.onError('文件上传失败,请求封装失败')
}
})
},
backfillLicence(type, fileId, path, status) {
if (type === 1) {
this.factoryFormData.bLicense = fileId
} else if (type === 2) {
this.factoryFormData.pLicense = fileId
}
if (status) {
this.backfillPicture(type, path)
}
},
backfillPicture(type, url) {
let arr = []
if (type === 1) {
arr = this.blFileList
} else {
arr = this.plFileList
}
const urlStr = url.split(',')
urlStr.forEach(item => {
const obj = {
url: item
}
arr.push(obj)
})
},
handleChange(file, fileList, status) {
if (status === 1) {
this.upload.bl = fileList.length >= this.limitNum
} else {
this.upload.pl = fileList.length >= this.limitNum
}
}
}
css
<style>
.hide .el-upload--picture-card {
display: none;
}
</style>