<el-form :model="bulletin" ref="bulletin" label-width="90px">
<el-form-item label="头像:">
<el-upload class="upload-demo upload"
drag
:show-file-list="false"
:action="uploadFilePath"
:data="activeCoverPhoto"
:before-upload="beforeUpload"
:on-success="activeCoverPhotoUploadSuccess">
<i v-if="!bulletin.cover" class="el-icon-upload"></i>
<div v-if="!bulletin.cover" class="el-upload__text">仅支持JPG/PNG,小于2M</div>
<img :src="getImgUrl(bulletin.cover)" alt="" style="width:100%;height:100%" v-if="bulletin.cover">
</el-upload>
</el-form-item>
</el-form>
// 上传图片携带数据
activeCoverPhoto: {
type: 40,
description: '杂志封面图'
},
// 图片上传成功返回图片路径
activeCoverPhotoImgUrl: '',
import { mapGetters } from 'vuex'
import { getImgUrl } from '@/utils'
computed: {
...mapGetters([
'uploadFilePath'
])
},
//封面开始
getImgUrl (uuid) {
return getImgUrl(uuid)
},
//图片
beforeUpload (file) {
const isType = file.type === 'image/jpeg' || file.type === 'image/png'
const isLt2M = file.size / 1024 / 1024 < 2
if (!isType) {
this.$message.error('图片只能是 JPG/PNG 格式')
}
if (!isLt2M) {
this.$message.error('图片大小不能超过 2MB')
}
return isType && isLt2M
},
activeCoverPhotoUploadSuccess (response, file, fileList) {
this.bulletin.cover = response.data
this.activeCoverPhotoImgUrl = URL.createObjectURL(file.raw)
},