图片选择/手动上传upload

form表单有关图片上传的部分

 <el-form-item label="图片选择" prop="cover">
          <p style="font-size: 12px; color: red; padding: 0; margin: 0">
            (注意:界面图片,自动上传;任选其一)
          </p>
          <el-radio v-model="imgChange" label="CHOOSE">选择图片</el-radio>
          <el-radio v-model="imgChange" label="UPLOAD">手动上传</el-radio>
          <div class="industryBox" v-if="imgChange == 'CHOOSE'">
            <div
              class="industryItem"
              v-for="(item, index) in industryList"
              :key="index"
            >
              <el-radio
                v-model="industryName"
                :label="item.logo"
                style="width: 80px;"
              >{{item.name}}</el-radio>
              <img
                :src="item.logo"
                alt=""
                style="width: 120px; height: 120px; object-fit: scale-down"
              />
            </div>
          </div>
          <el-upload
            v-else
            :file-list="selectFileList"
            :on-change="onchangeImg"
            :show-file-list="true"
            :on-remove="handleRemoveImg"
            class="editor-slide-upload"
            action
            :http-request="uploadImg()"
            list-type="picture-card"
            accept=".jpg,.png,.jpeg,.svg,.ico"
          >
            <i class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

页面呈现效果

 

业务需求如下

1.界面图片和手动上传任选其一,界面图片对应参数coverImage,手动上传更改时传cover,未更改coverImage;

2.添加或编辑的时候,需要将图片上传的方式

图片的校验

const validateLogoFile = (rule, value, callback) => {
      if(this.imgChange == 'CHOOSE') {
        if(this.industryName) {
          callback()
        }else {
          callback(new Error('请选择图片'))
        }
      }else {
        if (!this.selectFileList || this.selectFileList.length <= 0) {
          callback(new Error('请上传图片'))
        } else {
          callback()
        }
      }
    }


rules:{

 cover: [
          {
            required: true,
            validator: validateLogoFile,
            trigger: 'change',
          },
        ],

}

 勾选和手动上传的时候,对于图片的校验

watch:{
//手动上传
'imgChange': {
      handler(val) {
        this.$refs.ruleForm.clearValidate('cover')
      }
    },
//勾选
    industryName: {
      handler(val) {
        let index = this.industryList.findIndex(item => {
          return item.name == val
        })
        if (index > -1) {
          this.industryImg = this.industryList[index].logo
          this.$refs.ruleForm.clearValidate('cover') //清除文字校验
        } else {
          this.industryImg = ''
        }
      },
    },


}

数据的初始化

initDate(val){

     if(res.data.coverUploadWay == 'CHOOSE') {

            this.industryName = res.data.coverImage

          }else {

            this.selectFileList.push({ url: res.data.coverImage })

            this.industryNameClone = res.data.coverImage

          }

}

点击确认的时候,对数据进行进一步的处理

 if (this.CurObject.id) {
            if(this.imgChange == 'CHOOSE') {
              this.$set(obj,'coverImage',this.industryName)
            }else {
              if(this.imgFile) {
                this.$set(obj,'cover',this.imgFile)
              }else {
                this.$set(obj,'coverImage',this.industryNameClone)
              }
            }
            this.$set(obj, 'id', this.CurObject.id)
            let fd = new FormData()
            for (let item in obj) {
              fd.append(item, obj[item])
            }
            putArticle(fd).then(res => {
              this.flashParent()
            })
          } else {
            this.$set(obj, this.imgChange == 'CHOOSE' ? 'coverImage' :'cover', this.imgChange == 'CHOOSE' ? this.industryName : this.imgFile)
            let fd = new FormData()
            for (let item in obj) {
              fd.append(item, obj[item])
            }
            addArticle(fd).then(res => {
              this.flashParent()
            })
          }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
el-upload 是一个 Element UI 提供的组件,可以用来上传文件,其中也包括图片。在手动上传图片时,可以使用以下步骤: 1. 在模板中使用 el-upload 组件,并设置相应的属性: ```html <el-upload class="upload-demo" action="/upload" :on-success="handleSuccess" :before-upload="beforeUpload" :show-file-list="false"> <el-button size="small" type="primary">点击上传</el-button> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> ``` 其中,`action` 属性指定了上传文件的 URL,`on-success` 属性指定了上传成功后的回调函数,`before-upload` 属性指定了上传前的处理函数,`show-file-list` 属性设置为 `false`,表示不展示已上传的文件列表。 2. 在 Vue 实例中定义相应的方法: ```javascript export default { methods: { handleSuccess(response, file, fileList) { console.log(response, file, fileList); }, beforeUpload(file) { const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'; const isLt2M = file.size / 1024 / 1024 < 2; if (!isJPG) { this.$message.error('上传头像图片只能是 JPG 格式!'); } if (!isLt2M) { this.$message.error('上传头像图片大小不能超过 2MB!'); } return isJPG && isLt2M; }, }, }; ``` `handleSuccess` 方法会在上传成功后被调用,参数 `response` 表示服务端返回的数据,`file` 表示当前上传的文件,`fileList` 表示已上传的文件列表。 `beforeUpload` 方法会在上传前被调用,参数 `file` 表示当前要上传的文件。在该方法中,可以进行文件类型、大小等的校验。如果校验不通过,可以通过 `this.$message.error` 方法弹出错误提示。 注意:以上代码中的 `this.$message` 是 Element UI 提供的消息提示组件,需要在 Vue 实例中进行注册。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值