vue element-ui之图片上传el-upload

一、组件布局部分

<el-upload
      class="avatar-uploader"
      // action提交的地址为后台转为存放图片文件的一个接口地址
      action="/api/employee/addimages"
      :headers = "headers"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
       v-model="formEdit.e_icon">
       <img v-if="imageUrl" :src="imageUrl" class="avatar"/>
       <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>

二、js的methods部分(需要注意的是,file名是固定的,为后台的文件或图片名称,不能更改)

            //    图片上传
            handleAvatarSuccess(res,file) {
                this.imageUrl = URL.createObjectURL(file.raw);
                this.formEdit.e_icon = res.data[0].e_icon
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容