1. 程式人生 > >element-ui upload組件多文件上傳

element-ui upload組件多文件上傳

text bmi video upload span weight orm struct multi

之前有一篇寫的如何同時傳遞form表單及upload組件文件,如果有多個upload文件該如何傳遞呢

上代碼

html

 1         <el-form-item label="實驗信息" prop="expvmInstruction">
 2           <el-upload
 3             class="upload-demo"
 4             drag
 5             ref="uploadhtml"
 6             :action="upload_url"
 7             :auto-upload
="false" 8 :before-upload="newHtml" 9 accept=".html, .htm"> 10 <div class="el-upload__text">將文件拖到此處,或<em>點擊上傳</em></div> 11 <div slot="tip" class="el-upload__tip">實驗信息上傳,只能傳(.html/.htm)文件</div> 12 </el-upload
> 13 </el-form-item> 14 <el-form-item label="附件信息" prop="expvmFiles"> 15 <el-upload 16 class="upload-demo" 17 drag 18 ref="uploadfile" 19 :action="upload_url" 20 :auto-upload="false" 21 :before-upload
="newFiles" 22 multiple> 23 <div class="el-upload__text">將文件拖到此處,或<em>點擊上傳</em></div> 24 <div slot="tip" class="el-upload__tip">實驗信息附件上傳,只能傳(.file)文件</div> 25 </el-upload> 26 </el-form-item> 27 <el-form-item label="實驗視頻" prop="expvmVideo"> 28 <el-upload 29 class="upload-demo" 30 drag 31 ref="uploadvideo" 32 :action="upload_url" 33 :auto-upload="false" 34 :before-upload="newVideo" 35 accept=".mp4"> 36 <div class="el-upload__text">將文件拖到此處,或<em>點擊上傳</em></div> 37 <div slot="tip" class="el-upload__tip">實驗視頻上傳,只能傳(.mp4)文件</div> 38 </el-upload> 39 </el-form-item>

js

 1 newSubmitForm () {
 2       this.$refs[‘newform‘].validate((valid) => {
 3         if (valid) {
 4           this.uploadForm.append(‘expName‘, this.newform.expName)
 5           this.uploadForm.append(‘expSn‘, this.newform.expSn)
 6           this.uploadForm.append(‘groupId‘, this.newgroupId)
 7           this.uploadForm.append(‘subGroupId‘, this.newsubgroupId)
 8           this.uploadForm.append(‘expvmDifficulty‘, this.newform.expvmDifficulty)
 9           this.uploadForm.append(‘type‘, ‘0‘)
10           newExp(this.uploadForm).then(res => {
11             if (res.code === 400) {
12               this.$message.error(res.error)
13             } else if (res.code === 200) {
14               this.$message.success(‘上傳成功!‘)
15               this.showTableData()
16             }
17           })
18           this.$refs.uploadhtml.submit()
19           this.$refs.uploadfile.submit()
20           this.$refs.uploadvideo.submit()
21           this.newFormVisible = false
22         } else {
23           console.log(‘error submit!!‘)
24           return false
25         }
26       })
27     },
28     newHtml (file) {
29       this.uploadForm.append(‘html‘, file)
30       return false
31     },
32     newFiles (file) {
33       this.uploadForm.append(‘file[]‘, file)
34       return false
35     },
36     newVideo (file) {
37       this.uploadForm.append(‘video‘, file)
38       return false
39     }

在data中定義

uploadForm: new FormData(),

註意

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

這裏是接收多文件一定要是數組形式的file[]

element-ui upload組件多文件上傳