vue element upload 上传多个文件

element-ui upload组件多文件上传
之前有一篇写的如何同时传递form表单及upload组件文件,如果有多个upload文件该如何传递呢

上代码

html

按 Ctrl+C 复制代码

<el-form ref="newform" :model="newform" :rules="rules">
        <el-form-item prop="expName" label="">
          <el-input v-model="newform.expName" placeholder="" style="width:75%">
          </el-input>
        </el-form-item>
        <el-form-item prop="expSn" label="">
          <el-input v-model="newform.expSn" placeholder="" style="width:75%">
          </el-input>
        </el-form-item>
        <el-form-item label='' prop="groupName">
          <el-select v-model="newform.groupName" placeholder="" style="width:75%" @change="newSelectGroup($event)">
            <el-option
            v-for="item in groupOptions"
            :key="item.groupId"
            :label="item.groupName"
            :value="item.groupId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item  label="" prop="subGroupName">
          <el-select v-model="newform.subGroupName" placeholder="" style="width:75%" @change="newSelectSubGroup($event)">
            <el-option
            v-for="item in subGroupOptions"
            :key="item.subGroupId"
            :label="item.subGroupName"
            :value="item.subGroupId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="expvmDifficulty">
          <el-rate v-model="newform.expvmDifficulty" :max="5" style="line-height: 2;"></el-rate>
        </el-form-item>
        <el-form-item label="" prop="expvmInstruction">
          <el-upload
            class="upload-demo"
            drag
            ref="uploadhtml"
            :action="upload_url"
            :auto-upload="false"
            :before-upload="newHtml"
            accept=".html, .htm">
            <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
            <div slot="tip" class="el-upload__tip">实验信息上传,只能传(.html/.htm)文件</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="" prop="expvmFiles">
          <el-upload
            class="upload-demo"
            drag
            ref="uploadfile"
            :action="upload_url"
            :auto-upload="false"
            :before-upload="newFiles"
            multiple>
            <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
            <div slot="tip" class="el-upload__tip">实验信息附件上传,只能传(.file)文件</div>
          </el-upload>
        </el-form-item>
        <el-form-item label=""  prop="expvmVideo">
          <el-upload
            class="upload-demo"
            drag
            ref="uploadvideo"
            :action="upload_url"
            :auto-upload="false"
            :before-upload="newVideo"
            accept=".mp4">
            <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
            <div slot="tip" class="el-upload__tip">实验视频上传,只能传(.mp4)文件</div>
          </el-upload>
        </el-form-item>
        <el-form-item style="text-align:center">
         <el-button type="primary" @click="newSubmitForm()" class="yes-btn">
          确 定
         </el-button>
         <el-button @click="resetForm('newform')">
          重 置
         </el-button>
        </el-form-item>
     </el-form>
按 Ctrl+C 复制代码
js

按 Ctrl+C 复制代码

data () {
    return {
    upload_url: 'aaa',       // 随便填一个,但一定要有
     uploadForm: new FormData(),   // 一个formdata
      rules: {},     // 用到的规则
      newform: {
        expName: '',
        groupName: '',
        expSn: '',
        subGroupName: '',
        expvmDifficulty: 1
      }
    }
  }
按 Ctrl+C 复制代码
methods

按 Ctrl+C 复制代码

newSubmitForm () {
      this.$refs['newform'].validate((valid) => {
        if (valid) {
          this.uploadForm.append('expName', this.newform.expName)
          this.uploadForm.append('expSn', this.newform.expSn)
          this.uploadForm.append('groupId', this.newgroupId)
          this.uploadForm.append('subGroupId', this.newsubgroupId)
          this.uploadForm.append('expvmDifficulty', this.newform.expvmDifficulty)

          newExp(this.uploadForm).then(res => {
            if (res.code === 400) {
              this.$message.error(res.error)
            } else if (res.code === 200) {
              this.$message.success('上传成功!')

            }
          })
          this.$refs.uploadhtml.submit()   // 提交时触发了before-upload函数
          this.$refs.uploadfile.submit()
          this.$refs.uploadvideo.submit()

        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    newHtml (file) {   // before-upload
      this.uploadForm.append('html', file)
      return false
    },
    newFiles (file) {
      this.uploadForm.append('file[]', file)
      return false
    },
    newVideo (file) {
      this.uploadForm.append('video', file)
      return false
    }
按 Ctrl+C 复制代码
newExp函数是作为一个前后端交互的函数

按 Ctrl+C 复制代码

export function newExp (data) {
  return axios({
    method: 'post',  // 方式一定是post
    url: '你的后台接收函数路径',
    timeout: 20000,
    data: data        // 参数需要是单一的formData形式
  })
}
按 Ctrl+C 复制代码
PHP代码,后台接收

1 public function newExp() {
2     $param = $this->request->post();          // 获取页面表单传值
3     $files = $this->request->file();          // 接收到的文件
4 }
注意

this.uploadForm.append('file[]', file)

这里是接收多文件一定要是数组形式的file[]
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3.0 Element Upload 多图上传功能是一种非常方便的图片上传方式,可以通过简单的操作实现多张图片的批量上传,提高了用户的使用体验和工作效率。这种功能的实现需要依靠 Vue3.0 框架和 Element UI 组件库的支持。 首先,需要在 Vue3.0 项目中引入 Element UI 组件库,然后创建一个 Upload 组件实例,并设置相关属性,例如 action 属性表示上传地址、name 属性表示上传文件名、limit 属性表示上传文件大小限制、before-upload 属性表示上传前的校验等属性,然后在组件中添加一个 file-list 属性用来存储上传文件列表,以便在上传过程中监测文件上传进度和状态。 其次,需要在 Upload 组件中添加一个模板,用来定义上传文件列表和上传按钮等元素,通过对列表和按钮样式的设置,实现更满意的样式效果。在上传按钮的事件处理函数中,将上传文件列表传递给服务端,并监听上传进度和状态变化,通过回调函数的方式更新上传状态和提示信息。 最后,在 Vue3.0 项目中添加一些必要的插件和配置项,例如 Axios 库用于实现文件上传、MIME 类型设置、CORS 跨域请求设置等等,以满足上传功能的需求。同时,要注意安全性问题,例如对上传文件类型的限制、对上传文件大小的限制、对上传地址的设置等方面进行适当的处理。 综上所述,Vue3.0 Element Upload 多图上传功能的实现需要依靠 Vue3.0 框架和 Element UI 组件库的支持,通过设置相关属性、添加模板、绑定事件等方式实现。开发者需要注意安全性问题,并采用适当的插件和配置项,以便满足上传功能的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值