水越帆 2019-06-28 17:04 采纳率: 60%
浏览 7244
已采纳

vue+element-ui怎么把表单数据和图片一起提交到后端呢?能获取图片大小和名称么?

vue+element-ui怎么把表单数据和图片一起提交到后端呢?能获取图片大小和名称么?

  • 写回答

4条回答 默认 最新

  • 珍妮玛•黛金 博客专家认证 2019-06-29 11:14
    关注

    直接上代码吧,满意希望给我采纳,谢谢

    
      <el-dialog :title="'添加'" :visible.sync="formVisibleAdd" :close-on-click-modal="false" width="30%" @close="closeDialogAdd">
        <el-form :model="addForm" label-width="100px" :rules="rules" ref="addForm">
          <el-form-item label="名称" prop="name">
            <el-input v-model="addForm.name" placeholder="请输入名称" />
          </el-form-item>
    
          <el-form-item label="编号" prop="code">
            <el-input type="text" v-model="addForm.code" placeholder="请输入编号" />
          </el-form-item>
    
          <el-form-item label="图片">
            <el-upload class="upload-demo" accept="image/jpeg,image/jpg,image/png" :auto-upload="false" ref="upload" action="" :on-change="uploadSectionFile" :on-remove="removeImage" list-type="picture">
              <el-button size="small" type="primary" v-show="isUploadShowHide">点击上传</el-button>
              <div slot="tip" class="el-upload__tip" v-show="isUploadShowHide" style="margin-top: -40px;idth: 300px;margin-left: 100px;text-align: left;color: red;">只能上传jpg/png文件,且不超过500kb</div>
            </el-upload>
    
          </el-form-item>
    
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeDialogAdd">取消</el-button>
          <el-button type="primary" @click.native="handleSubmitAdd" :loading="formLoading">提交</el-button>
        </div>
      </el-dialog>
    
    
    let handleSubmitAdd = function() {
    
      if (this.formLoading) {
        return
      }
    
      this.$refs.addForm.validate(valid => {
        if (!valid) {
          return
        }
        this.formLoading = true
    
        var formData = new FormData;
        formData.append('name', this.addForm.name);
        formData.append('code', this.addForm.code);
        formData.append('file', this.uploadFile);
    
        let config = {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        };
    
        this.$axios.post('http://localhost:8090/xx/xx/insert', formData, config)
          .then((res) => {
            if (res.data.code === 0) {
    
              this.formLoading = false
              this.$refs.upload.clearFiles();
              this.formVisibleAdd = false
    
            }
          })
          .catch((res) => {
          })
      })
    
    }
    
    
    @PostMapping(value = "insert", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
        public CommResult insertTableTeam(HttpServletRequest request, 
                                                                                @RequestParam(value = "name", required = false) String name,
                                                                                @RequestParam(value = "code", required = false) String code,
                                          @RequestParam(value = "file", required = false) MultipartFile file) {
    
    
            String rootPath = request.getSession().getServletContext().getRealPath("/");
            if (file != null) {
                String fileName = file.getOriginalFilename();
                if (fileName != null && fileName.trim().length() > 0) {
                    String newFileName = FileUtil.uploadFile(rootPath, fileName, file);
    
                }
            }
                    //TODO 添加
            return "success";
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗
  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模