vue项目ElementUI组件中el-upload组件与图片裁剪功能组件结合使用

2023-11-14

vue项目ElementUI组件中el-upload组件与裁剪功能组件结合使用
如下图所示,点击上传组件,选择文件后,会立马弹出图片裁剪功能组件的页面
在这里插入图片描述
在这里插入图片描述

问题描述:
1,在使用upload组件中,如果修改fileList中的内容,浏览器会报错
2,获取上传的文件,传递给图片裁剪组件(在on-change中获取文件并传递个裁剪组件)
3,要获取裁剪后的图片即File文件(将裁剪后的图片返回出去)
4,获取到裁剪后的file调用上传的接口

由于el-upload组件默认使用的是
“选取文件后立即进行上传”,可通过auto-upload属性进行修改,将auto-upload设置为false;
同时也不显示已上传的文件列表,通过show-file-list属性修改,将show-file-list设置为false。

获取上传的组件说明:使用elementUI 提供的方法 on-change,获取已上传的组件

elementUI中upload组件部分属性如下:
在这里插入图片描述
关于裁剪组件,请看裁剪组件链接文档

本案例主要代码如下:

<el-form-item label="公司logo" prop="logo">
                    <el-upload
                            class="avatar-uploader"
                            ref="upload"
                            :action="uploadLogo"
                            :show-file-list="false"
                            :file-list="photoList"
                            :on-change="changePhotoFile"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload"
                            :headers="headerObj"
                            :auto-upload="false"
                    >
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <div v-else class=" avatar-uploader-icon">
                            <div>
                                <i  class="my-icon-plus"></i>
                                <p class="my-icon-word">添加</p>
                            </div>

                        </div>
                        <!--<i v-else class="el-icon-plus avatar-uploader-icon"></i>-->
                    </el-upload>
                    <my-cropper ref="myCropper" @getFile="getFile" @upAgain="upAgain"></my-cropper>
                </el-form-item>

在这里插入图片描述
对应的方法

 changePhotoFile(file, fileList){
                if (fileList.length > 0) {
                    this.photoList = [fileList[fileList.length - 1]]
                }
                this.handleCrop(file);
            },
   handleCrop(file){
                this.$nextTick(()=>{
                    this.$refs.myCropper.open(file.raw || file)
                })
            },
            // 点击弹框重新时触发
            upAgain(){
                this.$refs['upload'].$refs["upload-inner"].handleClick();
            },
            getFile(file){
                const formData = new FormData();
                formData.append("file",file)
                uploadSelfCompanyLogo(formData).then(res =>{
                    if (res.code === 0) {
                        this.companyInfo.logo = res.filename;
                        this.companyInfo.imageUrl = res.url;
                        this.imageUrl = res.url;
                        //上传成功后,关闭弹框组件
                        // this.handleCrop(file);
                        this.$refs.myCropper.close()

                    } else {
                        this.$message.error('上传出错');
                    }
                })
       
            },

整个vue代码,包含上面的代码

<template>
    <div class="form-out">
        <el-form
                :rules="rules"
                :model="companyInfo"
                class="form"
                ref="registForm"
                label-position="left"
                label-width="92px"
        >
            <div class="personal-info">
                <p class="tag">
                    <span class="light-slash">/</span>
                    <span class="slash">/</span>
                    <span class="tag-main">公司信息</span>
                    <span class="slash">/</span>
                    <span class="light-slash">/</span>
                </p>

                <el-form-item label="公司全称" prop="companyName">
                    <span class="com-name">{{companyInfo.companyName}}</span>
                </el-form-item>
                <el-form-item label="公司简称" prop="shortened">
                    <el-input
                            style="width: 540px;height: 42px"
                            v-model="companyInfo.shortened"
                            placeholder="请填写当前公司简称"
                            clearable
                    ></el-input>
                </el-form-item>
                <el-form-item label="行业领域" prop="industry">
                    <el-select
                            class="inputSelect"
                            v-model="companyInfo.industry"
                            placeholder="请选择行业领域"
                            clearable
                    >
                        <el-option
                                v-for="item in industryOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="公司规模" prop="scale">
                    <el-select
                            class="inputSelect"
                            v-model="companyInfo.scale"
                            placeholder="请选择公司规模"
                            clearable
                    >
                        <el-option
                                v-for="item in scaleOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="公司性质" prop="nature">
                    <el-select
                            v-model="companyInfo.nature"
                            placeholder="请选择公司性质"
                            clearable
                    >
                        <el-option
                                v-for="item in natureOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="公司logo" prop="logo">
                    <el-upload
                            class="avatar-uploader"
                            ref="upload"
                            :action="uploadLogo"
                            :show-file-list="false"
                            :file-list="photoList"
                            :on-change="changePhotoFile"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload"
                            :headers="headerObj"
                            :auto-upload="false"
                    >
                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                        <div v-else class=" avatar-uploader-icon">
                            <div>
                                <i  class="my-icon-plus"></i>
                                <p class="my-icon-word">添加</p>
                            </div>

                        </div>
                        <!--<i v-else class="el-icon-plus avatar-uploader-icon"></i>-->
                    </el-upload>
                    <my-cropper ref="myCropper" @getFile="getFile" @upAgain="upAgain"></my-cropper>
                </el-form-item>
                <el-form-item label="公司简介" prop="intro">
                    <el-input type="textarea" v-model="companyInfo.intro"></el-input>
                </el-form-item>
            </div>

            <div class="submit">
                <el-button
                        class="next-button"
                        type="primary"
                        size="medium"
                        :class="[nextFlag ?'next-button-bg-blue':'next-button-bg-grew']"
                        @click="companyRegister">
                    确定
                </el-button>
                <span class="return-back" @click="returnBackFun"> 返回上一步 </span>
            </div>
        </el-form>

    </div>
</template>

<script>
    import debounce from "../../../common/debounce";
    import DataDict from "../../../common/DataDict";
    import {createCompanyBase,uploadSelfCompanyLogo} from  '../../../network/request'
    import MyCropper from "./cropper.vue"
    export default {
        name: "ComFormThree",
        components:{MyCropper},
        data(){
            //信用代码验证
            var checkCreditCode = (rules, value, callback) => {
                let position = new RegExp(/^([0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}|[1-9]\d{14})$/);
                if(!position.test(value)) {
                    callback(new Error('请输入正确的统一社会信用代码!'));
                } else {
                    callback();
                }
            }
            return{
                nextFlag:false,
                uploadLogo: process.env.VUE_APP_BASE_URL + '/business/pub/iface/uploadCompanyLogo',//上传共公司logo地址
                imageUrl: '',
                companyInfo: {
                    companyName: '',
                    logo:'',
                    creditCode: '',
                    shortened: '',
                    industry:null,
                    scale:null,
                    nature:null,
                    companyId:null,
                    imageUrl:'',
                    intro:''
                },
                photoList:[],
                industryOptions:DataDict.industryOptions,//行业领域数据字典
                scaleOptions:DataDict.scaleOptions,//公司规模数据字典
                natureOptions:DataDict.natureOptions,//公司性质数据字典
                rules: {
                    companyName:[
                        { required: true},
                    ],
                    creditCode:[
                        { required: true, message: '请输入统一社会信用代码', trigger: 'blur' },
                        { validator: checkCreditCode, trigger: 'blur' }
                    ],
                    logo:[{ required: true},],
                    intro:[{ required: true, message: '请填写公司简介',trigger: 'blur' },],
                    shortened:[{ required: true, message: '请填写公司简称',trigger: 'blur'},],
                    industry:[{ required: true,
                        message: '请选择行业领域',
                        trigger: 'change'},],
                    scale: [
                        {
                            required: true,
                            message: '请选择公司规模',
                            trigger: 'change'
                        }
                    ],
                    nature: [
                        {
                            required: true,
                            message: '请选择公司性质',
                            trigger: 'change'
                        }
                    ],
                },
            }
        },
        mounted() {
           // console.log("this.$store.personInfo.companyName",this.$store.getters.personInfo.companyName);
                this.companyInfo.companyName = this.pCompanyName;
            },
        activated() {
            this.companyInfo.companyName = this.pCompanyName;
        },
        computed:{
            pCompanyName(){
                return this.$store.getters.personInfo.companyName;
            }
        },
     watch:{
         companyInfo: {
             handler(val){
                 (val.companyName !== ''
                     && val.creditCode !=='' && val.creditCode.length>0
                     && val.logo !== '' && val.logo.length>0
                     && val.shortened !== '' && val.shortened.length >0
                     && val.industry !== null
                     && val.nature !== null
                     && val.scale != null
                 )
                     ? this.nextFlag = true : this.nextFlag = false;
             },
             deep: true
         }
     },
        /*mounted() {
            console.log("uploadLogo====",this.uploadLogo);
        },*/
        methods:{
            //上传图片触发
            handleCrop(file){
                this.$nextTick(()=>{
                    this.$refs.myCropper.open(file.raw || file)
                })
            },
            // 点击弹框重新时触发
            upAgain(){
                this.$refs['upload'].$refs["upload-inner"].handleClick();
            },
            getFile(file){
                const formData = new FormData();
                formData.append("file",file)
                uploadSelfCompanyLogo(formData).then(res =>{
                    if (res.code === 0) {
                        this.companyInfo.logo = res.filename;
                        this.companyInfo.imageUrl = res.url;
                        this.imageUrl = res.url;
                        //上传成功后,关闭弹框组件
                        // this.handleCrop(file);
                        this.$refs.myCropper.close()

                    } else {
                        this.$message.error('上传出错');
                    }
                })
               // this.$refs.upload.submit();
            },
            companyRegister:debounce(function () {
                this.doCompanyRegister();
            },500),
            //下一步
            doCompanyRegister(){
                this.$store.commit('addcompanyObjInfo',this.companyInfo);
                createCompanyBase(this.companyInfo).then(res =>{
                    if (res.code === 0 && res.msg === 'success') {
                        console.log("创建成功");
                        /*this.$message.success('公司创建成功~');
                        this.$router.push("/client/auditPage");*/
                    }
                })
            },
            //头像上传成功之后的方法,进行回调
            handleAvatarSuccess(res,file) {
                if (res.code === 0) {
                    this.companyInfo.logo = res.filename;
                    this.companyInfo.imageUrl = res.url;
                    this.imageUrl = res.url;
                   // this.handleCrop(file);
                } else {
                    this.$message.error('上传出错');
                }
            },
            //上传图片时会被调用
            changePhotoFile(file, fileList){
                if (fileList.length > 0) {
                    this.photoList = [fileList[fileList.length - 1]]
                }
                this.handleCrop(file);
            },
            //头像上传之前的方法
            beforeAvatarUpload(file) {
                const isJPG =
                    file.type === 'image/jpeg' ||
                    'image/jpg' ||
                    'image/gif' ||
                    'image/png';
                const isLt6M = file.size / 1024 / 1024 < 6;

                if (!isJPG) {
                    this.$message.error(
                        '上传头像图片只能是 JPG、JPEG、GIF或PNG 格式!'
                    );
                }
                if (!isLt6M) {
                    this.$message.error('上传头像图片大小不能超过 6MB!');
                }
                console.log("275==",file)

                return isJPG && isLt6M;
            },
            //返回上一步
            returnBackFun(){
                let obj = {formType:3}
                this.$emit("returnBackTwo",obj)
            }
        }
    }
</script>

<style scoped lang="less">
    .form-out{
        width: 1100px;
        border-radius: 10px;
        background: #fff;
        .form {
            padding: 40px 120px;
            margin: 0 auto;
            display: table;
            .tag {
                text-align: center;
                margin: 0 0 40px 0;
                .tag-main{
                    display: inline-block;
                    font-size:16px;
                    font-family:PingFangSC-Semibold,PingFang SC;
                    font-weight:600;
                    color:#222222;
                    padding: 0 10px
                }
                .slash {
                    color: #437FFF;
                    font-weight: bold;
                    font-size: 16px;
                }
                .light-slash {
                    color: #437fff;
                    font-weight: bold;
                    font-size: 16px;
                    opacity: 0.5;
                }
            }
            .com-name{
                display: inline-block;
                /*width:224px;*/
                height:22px;
                font-size:16px;
                font-family:PingFangSC-Regular,PingFang SC;
                font-weight:400;
                color:rgba(51,51,51,1);
                line-height:22px;
            }
            .avatar {
                width: 80px;
                height: 80px;
              /*  border-radius: 30px;*/
                vertical-align: middle;
            }
            .avatar-desc {
                color: #999;
                font-size: 12px;
                padding-left: 10px;
            }
            .tag-other {
                margin-top: 40px;
            }
            .submit {
                display: flex;
                justify-content: center;
                margin-top: 40px;
                .submit-button {
                    background: #437FFF;
                    width: 390px;
                    margin-top: 20px;
                    font-size: 22px;
                }
                .next-button{
                    width:140px;
                    height:42px;
                    border-radius:6px;
                    font-size:16px;
                    color: #999999;
                    border: 1px solid transparent;
                    font-family:PingFangSC-Regular,PingFang SC;
                    font-weight:400;
                }
                .return-back{
                    display: inline-block;
                    height:42px;
                    line-height: 42px;
                    width:70px;
                    font-size:14px;
                    font-family:PingFangSC-Medium,PingFang SC;
                    font-weight:500;
                    color:rgba(102,102,102,1);
                    margin-left: 50px;
                    &:hover{
                        cursor: pointer;
                    }
                }
                .next-button-bg-grew{
                    background:#E5E5E5;
                }
                .next-button-bg-blue{
                    background:#437FFF;
                    color:#FFFFFF
                }

            }
        }
    }
</style>
<style scoped>

    .el-input__inner {
        width:540px;
        height:42px;
        background:rgba(249,249,249,1);
        border-radius:2px;
        border: 1px solid transparent;
    }
    .el-form-item__label{
        font-size:16px;
        font-family:PingFangSC-Regular,PingFang SC;
        font-weight:400;
        color:#222222;
    }

    input::-webkit-input-placeholder {
        height:21px;
        font-size:15px;
        font-weight:400;
        color:rgba(204,204,204,1);
        line-height:21px;
    }
    input::-ms-input-placeholder {
        height:21px;
        font-size:15px;
        font-weight:400;
        color:rgba(204,204,204,1);
        line-height:21px;
    }
    .my-icon-plus{
        background: url("../../../assets/img/upload/upload_plus.png") no-repeat;
        background-size: 24px 24px;
        width: 24px;
        height: 24px;
        display: inline-block;
    }
    .my-icon-word{
        height:17px;
        font-size:12px;
        font-family:PingFangSC-Regular,PingFang SC;
        font-weight:400;
        color:#437FFF;
        line-height:17px;
    }
</style>
<style >
    .form-out .avatar-uploader .el-upload {
        /*border: 1px dashed #d9d9d9;*/
        border: 1px dashed #437FFF;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
      /*  overflow: hidden;*/
    }
    .form-out .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .form-out .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 80px;
        height: 80px;
        text-align: center;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .form-out .avatar {
        width: 80px;
        height: 80px;
        display: block;
    }
</style>

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

vue项目ElementUI组件中el-upload组件与图片裁剪功能组件结合使用 的相关文章

随机推荐

  • 清明上河图30亿像素_清明上河图高清下载

    清摹本清明上河图高清全图 一亿像素 是为了方便众多学者们研究品评清明上河图的资源 小编这里给大家带来的是过亿像素的清摹本清明上河图高清全图 画质好到想假的一样 第一眼看到小编觉得以前看的清明上河图都是假的 如果你有需要的话 那就快来IT猫扑
  • 图片素材网站

    七大壁纸网站满足所有分辨率需求 如今手机电脑都是1080p起步 偏高端的2k 高端的4k都逐渐进入普通大众的接受范围 而电视机近两年不是4k都不好意思拿出手 虽然电视4k在今年这个时候对普通人来说也并不实用 我经常就为了找一些分辨率高的壁纸
  • 排序算法-【Java实现】-【桶排序、冒泡排序、快速排序、插入排序】

    排序算法 Java实现 桶 冒泡 快速 归并 插入排序 排序算法演示地址 https www cs usfca edu galles visualization ComparisonSort html 桶排序 顾名思义 将数组分到有限数量的
  • 有序链表转换二叉搜索树

    力扣入口 109 有序链表转化二叉搜索树 给定一个单链表 其中的元素按升序排序 将其转换为高度平衡的二叉搜索树 本题中 一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 思路加图解 思路1 class So
  • 【Python】dlib 无需编译安装 dlib-19.23.0-cp39-cp39-win_amd64.whl

    Dlib介绍 Dlib is a modern C toolkit containing machine learning algorithms and tools for creating complex software in C to
  • HTML5录音并调用百度语音识别

    HTML5录音借鉴的网上的代码 但是下载下来却无法用 查阅了好多资料 终于在国外某网站上找到原因 原来是js函数废弃了 替换为新的js函数名即可 HTML5录音的代码 http www it165 net design html 20140
  • opencv学习笔记十:使用cv2.morphologyEx()实现开运算,闭运算,礼帽与黑帽操作以及梯度运算

    openvc中morphologyEx 函数是一种形态学变化函数 数学形态学可以理解为一种滤波行为 因此也称为形态学滤波 滤波中用到的滤波器 kernal 在形态学中称为结构元素 结构元素往往是由一个特殊的形状构成 如线条 矩形 圆等 基本
  • Tomcat解决跨域问题

    Tomcat解决跨域问题 把下面的代码粘贴到web xml的552行下即可
  • 在 Dev-C++ 或 Code::Blocks 下面配置 EasyX

    前言 EasyX 虽然挺好用 但是目前官方只发布了针对 VC 的使用方法 本文介绍如何将 EasyX 配置到 DevCpp 或 CodeBlocks 里面 并提供相关的库 平时我工作忙 有问题直接在后面留言 我会尽力修改 注 版本太老的 m
  • java域名解析

    import java net InetAddress import java net UnknownHostException public class GetIp static public void main String args
  • Redis实现简单投票系统(微服务系列)

    package com jt redis import redis clients jedis Jedis import java lang reflect Member import java util Set public class
  • .NET网络编程——TCP通信

    一 网络编程的基本概念 1 网络 就是将不同区域的电脑连接到一起 组成局域网 城域网或广域网 把分部在不同地理区域的计算机于专门的外部设备用通信线路 互联成一个规模大 功能强的网络系统 从而使众多的计算机可以方便地互相传递信息 共享硬件 软
  • webrtc-stun/turn 服务器安装

    项目中用到了webrtc 他的p2p通过ICE实现 其中需要stun turn服务器接入 我用的google开源的 很好用 原来叫做turnserver 后来改名为corturn git有下载连接 其实安装 部署配置都很简单 有很多分享 重
  • Docker进阶学习:swarm集群搭建

    我们可以看一下官方文档关于工作机制的地方 How nodes work 可以从官方截图上看 manager顾名思义是管理器 但是管理器要做好分配 官方一句话 An N manager cluster tolerates the loss o
  • Java ArrayList 类

    ArrayList 类继承了 AbstractList 并实现了 List 接口 ArrayList 支持可以根据需要增长的动态数组 标准 Java 数组是固定长度的 数组创建后不能增长或缩小 这意味着我们必须提前知道一个数组将包含多少个元
  • 5年测试开发,跳槽薪资25k变成30k,总结的这些面试题,你会哪些?

    每年的金三银四都是各大公司招聘程序员的最佳时期 在这段时间内有好多程序员会为了面试而发愁 不知道如何才能收到好的offer 拿到理想的薪资 实现自我的人生价值 我想告诉大家的是 其实都不用愁的 好好准备一下就可以了 每个人都想找一份大厂的
  • 算法题汇总

    NO1 打靶问题 打十次打靶 总分数为90分的情况有哪些 NO2 阶乘末尾0的个数 NO1 打靶问题 打十次打靶 总分数为90分的情况有哪些 个人感觉比较像一类排列组合递归回溯的解法 package com lzg flume interc
  • QProcess创建子进程的方法,以及将子进程窗口嵌入主进程窗口中

    QProcess创建子进程的方法
  • Linux中的ftp

    引言 FTP 是因特网网络上历史最悠久的网络工具 从 1971 年由 A KBHUSHAN 提出第一个 FTP 的RFC RFC114 至今近半个世纪来 FTP 凭借其独特的优势一直都是因特网中最重要 最广泛的服务之一 一 FTP概述 FT
  • vue项目ElementUI组件中el-upload组件与图片裁剪功能组件结合使用

    vue项目ElementUI组件中el upload组件与裁剪功能组件结合使用 如下图所示 点击上传组件 选择文件后 会立马弹出图片裁剪功能组件的页面 问题描述 1 在使用upload组件中 如果修改fileList中的内容 浏览器会报错