首页 > 其他分享 >在vue中用multipart/form-data方式上传文件并同表单同步提交数据

在vue中用multipart/form-data方式上传文件并同表单同步提交数据

时间:2023-02-10 11:45:59浏览次数:50  
标签:vue false form uploadFile 上传 forms multipart data viewDataForm

项目是基于若依开发

1、dialog页面内容

 <el-dialog :title="viewDataTitle" :visible.sync="viewDataOpen" width="600px" append-to-body>
      <el-form ref="viewDataForm" :model="viewDataForm" label-width="100px">
        <el-form-item label="视图选择" prop="type">
          <el-select v-model="viewDataForm.type" placeholder="请选择视图" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in typeOptions" :key="index" :label="item.label"
                       :value="item.value" @click.native="seletChange(item)" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="视图名称" prop="viewName" v-if="isShowFir">
          <el-input v-model="viewDataForm.viewName" placeholder="请输入视图名称" />
        </el-form-item>
        <el-form-item label="导入文件" prop="uploadFile" v-show="isShowSec">
          <el-upload class="upload-demo" v-model="viewDataForm.uploadFile" ref="viewDataUpload" 
:limit="1" action="uploadAction" :data="viewDataForm" :headers="viewDataUpload.headers"
:on-exceed="viewHandleExceed"
:auto-upload="false"
:on-error="viewUploadError"
:before-upload="viewHandleBeforeUpload" :on-change="changeFile" :on-remove="viewHandleRemove" :file-list="uploadFile"> <el-button plain size="mini" type="primary">选择文件</el-button> </el-upload> </el-form-item> <el-form-item label="数据" prop="viewVal" v-show="isShowThird"> <el-input v-model="viewDataForm.viewVal" placeholder="请输入数据" /> </el-form-item> <div slot="footer" class="dialog-footer"> <el-button type="primary" @click="viewDataSubmitForm">确 定</el-button> <el-button @click="viewDataCancel">取 消</el-button> </div> </el-dialog>

2、data部分

   //是否显示外省弹出层
   viewDataOpen: false,
   //外省弹出层标题
   viewDataTitle: "",
   //外省文件上传
   viewDataUpload: {
       headers: {
             "Content-Type": "multipart/form-data",
             Authorization: "Bearer " + getToken()
       },
   },
   uploadFile: [],
   //表单参数
   viewDataForm:{},

3、methods

 viewDataHandleUpload() {
            this.viewReset();
            this.viewDataOpen = true;
            this.viewDataTitle = "首页视图数据录入";
            -----省略-----
          },
          viewHandleExceed(files, fileList) {
            this.$message.error("只能长传一个文件!");
          },
          viewHandleRemove(file, fileList) {
            this.$message.error("删除成功!");
          },
          // 图片上传失败时
          viewUploadError() {
            this.$message.error("图片上传失败!");
          },
          //上传之前
          viewHandleBeforeUpload(file) {

          },
          changeFile(file, fileList) {
            this.viewDataForm.uploadFile = fileList[0].raw
          },
          // 表单重置
          viewReset() {
            this.viewDataForm = {
              type: null,
              viewName: null,
              viewVal: null,
              --------省略-------
            };
            this.uploadFile = [];
          },
          viewDataCancel() {
            this.viewDataOpen = false;
            this.viewReset();
          },
          seletChange(data) {
              --------省略----------
          },
          viewDataSubmitForm() {
            this.$refs["viewDataForm"].validate(valid => {
              if (valid) {
                let forms = new FormData();
                //该数据类型默认参数Content-Type:multipart/form-data
                forms.append("uploadFile",this.viewDataForm.uploadFile);
                forms.append("type", this.viewDataForm.type);
                forms.append("viewName", this.viewDataForm.viewName);
                forms.append("viewVal", this.viewDataForm.viewVal);
                saveViewData(forms).then(response => {
                    this.$modal.msgSuccess("录入成功");
                    this.viewDataOpen = false;
                  });
              }
            });
          },

4.后台接收

@PostMapping("/saveView")
public AjaxResult saveView(ViewDataVo viewData,@RequestParam(value = "uploadFile",required = false) MultipartFile uploadFile) {
    --------省略----------
}

  

  

  

标签:vue,false,form,uploadFile,上传,forms,multipart,data,viewDataForm
From: https://www.cnblogs.com/person008/p/17108373.html

相关文章

  • Vue cli 安装
    1.安装Node.js2.配置npm源  npmconfigsetregistryhttps://registry.npmmirror.com当配置成功后,可通过get命令可查看npm源:npmconfiggetregistry......
  • VUE与UE5 像素流送
    1:首先安装PixelStreaming插件 启动后需重启2:偏好设置额外启动参数:-AudioMixer-PixelStreamingIP=localhost-PixelStreamingPort=88883:打包window4:打包结束后,将......
  • vue2中diff算法
    答:参考文献:https://blog.csdn.net/Curry_On/article/details/125376479......
  • vue关于this.a=this.b,改变this.b的数据问题
    因为需要对请求到的数据进行处理后在渲染到视图上,所以借助handleDate变量来对原始的数据data2进行处理,methods:{//点击实时概况hidden(){letpanel......
  • 54Oracle日期格式(NLS_DATE_FORMAT)介绍
    正常情况下Oracle默认显示的日期格式SYS@PROD1>selectsysdatefromdual;SYSDATE---------22-DEC-17这样显示不方便,如果想要显示的更加详细一些的话,可用通过设置N......
  • vue3单页抽取api
    编写apiimportaxiosfrom'~/util/axios'constfindByPage=(data)=>{returnaxios.post("/emo/page",data)}constlogin=(data)=>{returnhttp.p......
  • mysql ERROR 1682 (HY000): Native table 'performance_schema'.'global_variables' h
    mysql数据库出现这个问题 解决办法mysql_upgrade-uroot-p  修复完成之后回复正常 ......
  • Vue框架背后的故事
    前言Vue框架作为当前主流的前端框架之一,作者尤雨溪更是国内开发者心中男神,作为开发工程师你是否知道Vue框架名字的由来?你是否知道Vue框架作者尤大大创作Vue过程中的几个节点......
  • 前端-vue基础44-综合案例图书管理3补充新的知识
      <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><t......
  • 前端-vue基础48-综合案例图书管理7修改数据
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Do......