vue element upload 上传多个文件

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

上代码

html

按 Ctrl+C 复制代码

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

你可能感兴趣的:(vue,element,上传多个文件,前端)