您好,登錄后才能下訂單哦!
之前有一篇寫的如何同時傳遞form表單及upload組件文件,如果有多個upload文件該如何傳遞呢
上代碼
html
<el-form ref="newform" :model="newform" :rules="rules"> <el-form-item prop="expName" label=""> <el-input v-model="newform.expName" placeholder="" > </el-input> </el-form-item> <el-form-item prop="expSn" label=""> <el-input v-model="newform.expSn" placeholder="" > </el-input> </el-form-item> <el-form-item label='' prop="groupName"> <el-select v-model="newform.groupName" placeholder="" @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="" @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" ></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 > <el-button type="primary" @click="newSubmitForm()" class="yes-btn"> 確 定 </el-button> <el-button @click="resetForm('newform')"> 重 置 </el-button> </el-form-item> </el-form>
js
data () { return { upload_url: 'aaa', // 隨便填一個,但一定要有 uploadForm: new FormData(), // 一個formdata rules: {}, // 用到的規則 newform: { expName: '', groupName: '', expSn: '', subGroupName: '', expvmDifficulty: 1 } } }
methods
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 } newExp函數是作為一個前后端交互的函數 export function newExp (data) { return axios({ method: 'post', // 方式一定是post url: '你的后臺接收函數路徑', timeout: 20000, data: data // 參數需要是單一的formData形式 }) }
PHP代碼,后臺接收
public function newExp() { $param = $this->request->post(); // 獲取頁面表單傳值 $files = $this->request->file(); // 接收到的文件 }
注意
this.uploadForm.append('file[]', file)
這里是接收多文件一定要是數組形式的file[]
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。