vue el-upload 组件上传的 文件跟随表单数据 发送后台保存

13 篇文章 0 订阅
5 篇文章 0 订阅

碰到个业务需求,表单项有图片或文件上传需求,并且要求跟随表单数据一起走...

上传用element的el-upload组件

表单数据带着文件时,表单上传格式需要变动,需要使用 FormData 对象 ,而文件需要设置手动上传,并把获取到的文件添加到表单对象中,并把其他对单数据也逐步添加到表单对象

先介绍获取上传文件的方式,上传部分代码:

     <el-row style="width:500px;height:200px;float:right;">
        <el-col>
      <el-form-item label="用户头像"
                    prop="headUrl">
        <el-upload action=""
                   class="avatar-uploader"
                   :show-file-list="false"
                   ref="uploadImage"
                   :http-request="uploadImg"
                   :multiple="false"
                   :auto-upload="false"
                   :on-change="imgSaveToUrl"
                   :before-upload="beforeAvatarUpload"
                   accept="image/png,image/jpg,image/jpeg">
          <img v-if="dataForm.headUrl"
               :src="dataForm.headUrl"
               class="avatar" />
          <i v-else
             class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      </el-col>
      </el-row>

 上传部分样式:

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

上传组件 action 为上传地址,这里是跟随表单走,所以设置为空,但action 必须有!!!,另外设置自动上传为 false,也可以设置多文件,或者列表形式上传或者设置文件格式和大小拦截,这里重点是 

:http-request="uploadImg"

这个属性绑定一个方法就是自定义上传方法,我们就是通过这个方法获取准备上传的文件,其他事件获取的文件用于上传时有问题。

注意:这个自定义方法是在组件使用submit后才会触发(大坑,折腾了好久才发现) 

上传前文件格式和大小判定代码:

beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg' || 'image/png' || 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG,PNG,JPEG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
 },

靠这个方法绑定change事件实现自定义图片回显:

    imgSaveToUrl (event) {
      if (this.beforeAvatarUpload(event)) {
        var URL = null
        if (window.createObjectURL != undefined) {
          // basic
          URL = window.createObjectURL(event.raw)
        } else if (window.URL != undefined) {
          // mozilla(firefox)
          URL = window.URL.createObjectURL(event.raw)
        } else if (window.webkitURL != undefined) {
          // webkit or chrome
          URL = window.webkitURL.createObjectURL(event.raw)
        }
        this.dataForm.headUrl = URL
      }
    },

其他详细看代码:

	//自定义上传方法,使用上传组件的submit()后才会触发以获取文件实体
    uploadImg (param) {
      this.img = param.file
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs.dataForm.validate((valid) => {
          if (!valid) {
            return false
          }
          this.$refs.uploadImage.submit() //必须使用此方法才会触发自定义上传方法以获取文件对象
          var form = new FormData() //携带文件必须使用此对象
          if (this.img) {
            form.append("file", this.img) 把文件实体添加到表单对象
          }
          let obj = {
            id: this.dataForm.id || undefined,
            username: this.dataForm.username,
            password: this.dataForm.password,
            confirmPassword: this.dataForm.confirmPassword,
            realName: this.dataForm.realName,
            idCard: this.dataForm.idCard,
            gender: this.dataForm.gender,
            email: this.dataForm.email,
            mobile: this.dataForm.mobile,
            mobile2: this.dataForm.mobile2,
            telephone: this.dataForm.telephone,
            wechat: this.dataForm.wechat,
            passportNum: this.dataForm.passportNum,
            appNum: this.dataForm.appNum,
            status: this.dataForm.status,
            appStatus: this.dataForm.appStatus,
            spell: this.dataForm.spell,
            pinyin: this.dataForm.pinyin,
            sort: this.dataForm.sort,
            superAdmin: this.dataForm.superAdmin,
            createDate: null,
            headUrl: this.dataForm.headUrl,
            masterDeptIdArr: this.dataForm.masterDeptIdArr,
            masterJobId: this.dataForm.masterJobId,
            slaveDeptJobList: this.dataForm.slaveDeptJobList
          }
		  //序列化其他数据为json添加到表单对象,后台反序列化获取对象实体,也可以单个append(),后台直接用对象取,如果一次性添加而不序列化会出错
          form.append("dto", JSON.stringify(obj)) 

          api.saveUser(form)
            .then((res) => {
              this.$message({
                message: this.$t('prompt.success'),
                type: 'success',
                duration: 500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                },
              })
            })
            .catch(() => { })
        })
      },
      1000,
      { leading: true, trailing: false }
    ),

后台接收:

	@PostMapping
	@ApiOperation("保存")
	@LogOperation("保存")
	@RequiresPermissions("sys:user:save")
	public Result save(@RequestParam(required = false,name = "file") MultipartFile file, @RequestParam(name = "dto") String dto){
		JSONObject jsonObject = JSONObject.parseObject(dto);
		SysUserDTO sysUserDTO = JSONObject.parseObject(dto, SysUserDTO.class);
		
        //效验数据
		ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
		
		if (file != null) {
			MultipartFile []files =new MultipartFile[1];
			files[0] = file;
			String headUrl = FileUtils.saveFile(files);
			sysUserDTO.setHeadUrl(headUrl);
		}
		sysUserService.save(sysUserDTO);

		return new Result();
	}

 

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue 3 使用 el-upload 组件进行视频上传的步骤如下: 1. 首先,确保已经安装了 Element-UI 并正确引入了相关组件。 2. 在 Vue 组件,使用 el-upload 组件来创建文件上传表单: ```html <template> <el-upload class="upload-demo" action="/your-upload-url" :on-success="handleSuccess" :before-upload="beforeUpload" :auto-upload="false" :file-list="fileList" :limit="1" accept="video/*" > <el-button size="small" type="primary">点击上传视频</el-button> <div slot="tip" class="el-upload__tip">只能上传一个视频文件</div> </el-upload> </template> ``` 在上面的示例,我们设置了以下属性: - `action`:设置上传的 URL,将视频文件发送到服务器的该地址。 - `on-success`:设置上传成功后的回调函数,处理上传成功的逻辑。 - `before-upload`:设置上传之前的回调函数,可以在此处进行一些验证操作,例如文件大小、文件类型等。 - `auto-upload`:设置是否自动上传,默认为自动上传。 - `file-list`:绑定上传文件列表的数组,在 `handleSuccess` 回调函数可以更新该数组。 - `limit`:限制上传文件数量为 1。 - `accept`:设置接受的文件类型为 video/*,即只允许上传视频文件。 3. 在 Vue 组件的 `methods` 定义相关方法: ```javascript methods: { handleSuccess(response, file, fileList) { // 处理上传成功的逻辑 console.log(response); }, beforeUpload(file) { // 进行文件验证操作,例如文件大小、类型等 console.log(file); }, }, ``` 在 `handleSuccess` 方法,可以处理上传成功后的逻辑,例如更新文件列表、显示上传成功的提示信息等。 在 `beforeUpload` 方法,可以进行文件验证操作,例如判断文件类型、大小等。 请注意,上述示例的 `/your-upload-url` 需要替换为你自己的上传地址。 希望这能帮到你!如果还有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值