vue-element表单内使用上传文件,并和表单其他内容一起上传

3 篇文章 0 订阅
1 篇文章 0 订阅

vue-element 上传文件


表单内使用上传文件,并和表单其他内容一起上传


<template>
  <div class="addAppUpdate">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="版本号" prop="version">
        <el-input v-model="ruleForm.version" style="width: 200px;"></el-input>
      </el-form-item>
      <el-form-item label="更新日志" prop="log">
        <el-input type="textarea" v-model="ruleForm.log" style="width:500px;"></el-input>
      </el-form-item>
      <el-form-item label="上传文件" prop="apk">
        <el-upload ref="upload" class="upload-demo" action="/manager/appupdate/addAppUpdate" :headers="headers"
          :http-request="httpRequest" :before-remove="beforeRemove" :before-upload="beforeUploadFile" :on-exceed="handleExceed"
          multiple :limit="1" :auto-upload="false" :on-change="getFile" :data="ruleForm" :file-list="fileList" name="annexFile"
          style="width: 500px;">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">{{message}}</div>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')" v-loading.fullscreen.lock="fullscreenLoading">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import {
    addAppUpdate
  } from "../../api/api.js"
  export default {
    name: 'AddAppUpdate',
    data() {
      return {
        fullscreenLoading: false,
        //表单
        ruleForm: {
          version: '', //版本号
          log: '' //更新日志
        },
        //上传
        headers: {
          token: getStore('zxdAdmintoken'),
          "content-type": "multipart/form-data"
        },
        message: '请上传apk文件',
        fileList: [], //文件列表
        fd: {}, //用于放数据  FormData类型

        //校验规则
        rules: {
          version: [{
            required: true,
            message: '请输入版本号',
            trigger: 'blur'
          }, ],
          log: [{
            required: true,
            message: '请填写更新日志',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      //上传
      getFile(file, fileList) {
        this.fileList = fileList;
        // console.log(this.fileList)
        const fd = new FormData() // FormData 对象
        this.fd = fd
      },
      //上传前
      beforeUploadFile(file) {
        let extension = file.name.substring(file.name.lastIndexOf('.') + 1);
        console.log(extension)
        if (extension !== 'apk') {
          // this.$message.warning('只能上传后缀是.zip/.rar/.apk的文件'); //控制文件类型
          this.$message.warning('文件类型不对'); //控制文件类型
          return false
        }
      },
      //超限制
      handleExceed(files, fileList) {
        this.$message.warning("目前只能上传一个包")
      },
      //移除
      beforeRemove(file, fileList) {
        let extension = file.name.substring(file.name.lastIndexOf('.') + 1);
        if (extension !== 'apk') {
          return
        }
        return this.$confirm(`确定移除 ${file.name}?`)
      },
      httpRequest(param) {
        const fileObj = param.file // 相当于input里取得的files
        this.fd.append('apk', fileObj) // 文件对象
        console.log("文件包" + this.fd.get('apk'));
      },
      //提交
      submitForm(formName) {
        // let fileArr = this.$refs.upload.uploadFiles;
        // console.log(fileArr)
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.fileList.length <= 0) {
              this.$message.error("至少上传一个包!");
              return;
            }
            this.$refs.upload.submit();
            //换行自动添加为<br/>
            this.ruleForm.log= this.ruleForm.log.replace(/\n/g,"<br/>");
            // console.log(this.ruleForm.log)
            //将表单内其他内容添加进fd
            this.fd.append('version', this.ruleForm.version)
            this.fd.append('log', this.ruleForm.log)
            this.fd.append('type', "0")
            this.fullscreenLoading = true;
            //调用后端接口,提交即可
            addAppUpdate(this.fd).then(data => {
              console.log(data)
              if (data.code == 0) {
                this.fullscreenLoading = false;
                this.$message({
                  message: '上传成功',
                  type: 'success'
                });
                this.fd = {}
                this.fileList = []
                resetForm(formName)
              } else {
                this.$message.error("上传失败");
              }
            })
          } else {
            // console.log('error submit!!');
            return false;
          }
        });
      },
      //重置
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.$refs.upload.clearFiles()
      },

    }
  }
</script>

页面显示
在这里插入图片描述

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在Vue使用Element UI的表单和拖拽文件上传功能,你需要结合Element UI的Upload组件和拖放API来实现。以下是一个示例: 1. 首先,确保已经安装并引入了Element UI和相应的样式。 2. 在Vue组件中,使用Element UI的Upload组件,并添加一个拖拽区域: ```html <template> <el-upload action="/upload" :on-success="handleSuccess" :before-upload="beforeUpload" :auto-upload="false" drag multiple > <i class="el-icon-upload"></i> <div class="el-upload__text">将文件拖到此处,或点击上传</div> </el-upload> </template> ``` 在这个示例中,我们使用了`el-upload`组件,并启用了拖拽功能(`drag`)和多文件上传(`multiple`)。你需要根据实际需求,设置`action`、`on-success`、`before-upload`和其他相关属性。 3. 在Vue组件的`methods`中定义处理上传成功和上传前的方法: ```javascript <script> export default { methods: { handleSuccess(response, file, fileList) { // 处理上传成功的逻辑 // response为服务器返回的响应数据 }, beforeUpload(file) { // 处理上传前的逻辑 // 返回false可以阻止文件上传 // 返回Promise可以进行异步操作,比如文件校验等 } } }; </script> ``` 在上述示例中,`handleSuccess`方法会在文件上传成功后被调用,你可以在此方法中处理上传成功后的逻辑。`beforeUpload`方法会在文件上传前被调用,你可以在此方法中进行上传前的逻辑处理。你可以根据需要修改这两个方法的具体实现。 4. 在CSS中为拖拽区域添加样式: ```css <style> .el-upload-dragger { border: 2px dashed #ccc; text-align: center; padding: 20px; } </style> ``` 这样,你就可以在Vue使用Element UI的表单和拖拽文件上传功能了。拖拽区域会显示一个上传图标和提示文字,用户可以将文件拖拽到该区域进行上传上传文件会发送到指定的服务器地址,并在上传成功后调用`handleSuccess`方法进行处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值