vue-element-admin之el-form表单上传多个文件,并与表单一起提交

elementUI+vue表单上传多个文件和其他表单数据一起提交

这里使用的formdata提交方式,this.$refs.upload.submit()提交会触发http-request自定义上传方法

element代码

<el-dialog title="文件上传" :visible.sync="uploadVisible" width="650px">
      <el-form ref="ruleForm" :model="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="上传附件" prop="annexFile">
          <el-upload
            ref="upload"
            class="upload-demo"
            action="/api/boAnnex/addBoAnnex"
            :http-request="httpRequest"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :on-exceed="handleExceed"
            multiple
            :limit="10"
            :auto-upload="false"
            :on-change="getFile"
            :data="ruleForm"
            :file-list="fileList"
            name="annexFile"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="ruleForm.remark" type="textarea" rows="5" placeholder="请输入内容" maxlength="300" show-word-limit style="width: 400px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
      </div>
    </el-dialog>

data数据

data() {
    return {
      uploadVisible: false,
      ruleForm: {
        remark: ''
      },
      fileList: [],
      fd: {}
    }
  },

调用的methods

	getFile(file, fileList) {
      const fd = new FormData()// FormData 对象
      this.fd = fd
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 10 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`)
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}?`)
    },
    resetForm(formName) {
      this.uploadVisible = false
      this.$refs[formName].resetFields()
      this.$refs.upload.clearFiles()
    },
    httpRequest(param) {
      const fileObj = param.file // 相当于input里取得的files
      this.fd.append('annexFileArray', fileObj)// 文件对象
    },
    submitForm(file) {
      this.$refs.upload.submit()
      this.fd.append('remark', this.ruleForm.remark)
      this.fd.append('boId', this.businessId)
      uploadAnnex(this.fd).then(res => {
        this.uploadVisible = false
        this.$message({
          message: '上传成功',
          type: 'success'
        })
        this.getAnnexList()
      })
    },

上传接口代码

export function uploadAnnex(data) {
  return request({
    url: '/api/boAnnex/addBoAnnex',
    method: 'post',
    data: data
  })
}

以上就可以了~~~~~~~~~~~~~~~~~~~

  • 0
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue-element-admin 是一个基于 Vue.js 和 Element UI 的后台管理系统框架,它提供了一套完整的解决方案来构建现代化的后台管理应用程序。 在 Vue-element-admin 中,登录页面是系统的入口点,用于用户登录和身份验证。该登录页面通常包含一个表单,要求输入用户名和密码。用户输入正确的凭据后,系统会验证用户身份并进行登录操作。 以下是一个简单的示例代码,展示了如何创建一个基本的登录页面: ``` <template> <div class="login"> <el-form ref="loginForm" :model="loginForm" :rules="rules" label-width="80px"> <el-form-item label="用户名" prop="username"> <el-input v-model="loginForm.username"></el-input> </el-form-item> <el-form-item label="密码" prop="password"> <el-input type="password" v-model="loginForm.password"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="login">登录</el-button> </el-form-item> </el-form> </div> </template> <script> export default { data() { return { loginForm: { username: '', password: '' }, rules: { username: [ { required: true, message: '请输入用户名', trigger: 'blur' } ], password: [ { required: true, message: '请输入密码', trigger: 'blur' } ] } }; }, methods: { login() { this.$refs.loginForm.validate((valid) => { if (valid) { // 执行登录操作 console.log('登录成功'); } else { console.log('表单验证失败'); return false; } }); } } }; </script> ``` 在上述示例中,我们使用了 Element UI 的表单组件来创建输入框,并使用了表单验证规则来验证用户输入。当用户点击登录按钮时,会触发 `login` 方法进行表单验证。如果验证通过,我们可以执行登录操作。在这个示例中,我们只是简单地打印了一条成功或失败的消息。 请注意,这只是一个简单的示例,实际的登录页面可能需要更复杂的逻辑和后端 API 调用来进行身份验证。你可以根据自己的需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值