el-upload http-request使用 多個(gè)文件上傳攜帶其他參數(shù)方式
el-upload http-request使用
1、http-request:覆蓋默認(rèn)的上傳行為,可以自定義上傳的實(shí)現(xiàn)
2、不需要給action關(guān)聯(lián)上傳的地址,但是action這個(gè)屬性必須寫
3、在http-request自定義上傳事件中將上傳文件保存到一個(gè)數(shù)組中
4、 接口使用formData表單數(shù)據(jù)格式傳參, 將上傳的文件和其他參數(shù)一起傳給接口,使用的方法是new FormData()
4、如果是單文件上傳并攜帶參數(shù),可以直接在http-request自定義事件中調(diào)用后端接口
多文件上傳攜帶參數(shù)完整代碼參考如下:
<template> ? <div> ? ? <el-form ? ? ? :model="formData" ? ? ? label-width="80px" ? ? > ? ? ? <el-form-item ? ? ? ? label="科目名稱" ? ? ? ? prop="name" ? ? ? > ? ? ? ? <el-input v-model="formData.name" /> ? ? ? </el-form-item> ? ? ? <el-form-item label="上傳文件"> ? ? ? ? <el-upload ? ? ? ? ? action ? ? ? ? ? :http-request="httpRequest" ? ? ? ? ? :before-upload="beforeUploadClick" ? ? ? ? ? :on-exceed="handleExceed" ? ? ? ? ? :limit="2" ? ? ? ? ? multiple ? ? ? ? ? :show-file-list="false" ? ? ? ? > ? ? ? ? ? <el-button ? ? ? ? ? ? slot="trigger" ? ? ? ? ? ? size="small" ? ? ? ? ? ? type="primary" ? ? ? ? ? >選取文件</el-button> ? ? ? ? </el-upload> ? ? ? </el-form-item> ? ? ? <el-form-item> ? ? ? ? <el-button ? ? ? ? ? type="primary" ? ? ? ? ? @click="saveClick" ? ? ? ? >保存</el-button> ? ? ? </el-form-item> ? ? </el-form> ? </div> </template>
<script> import axios from 'axios' export default { ? data() { ? ? return { ? ? ? formData: { name: '' }, ? ? ? fileList: [] ? ? } ? }, ? methods: { ? ? // 重寫文件上傳方法,覆蓋原有的上傳方法,將上傳的文件依次添加到fileList數(shù)組中 ? ? httpRequest(option) { ? ? ? this.fileList.push(option) ? ? }, ? ? ?// 上傳前處理 ? ? beforeUploadClick(file) { ? ? }, ? ? // 文件數(shù)量過多時(shí)提醒 ? ? handleExceed() { ? ? ? this.$message({ type: 'info', message: '最多支持2個(gè)附件上傳' }) ? ? }, ? ? // 表單提交 ? ? async saveClick() { ? ? ? // 使用form表單的數(shù)據(jù)格式 ? ? ? const paramsData = new FormData() ? ? ? // 將上傳文件數(shù)組依次添加到參數(shù)paramsData中 ? ? ? this.fileList.forEach((it, index) => { ? ? ? ? paramsData.append(`file${index}`, it.file) ? ? ? }); ? ? ? // 將表單數(shù)據(jù)添加到參數(shù)paramsData中 ? ? ? paramsData.append('name', this.formData.name) ? ? ? // 表單數(shù)據(jù)和文件一起上傳,調(diào)用后端接口 ? ? ? const res = await axios({ ? ? ? ? url: '后端接口地址', ? ? ? ? method: 'post', ? ? ? ? data: paramsData ? ? ? }) ? ? } ? } } </script>
el-upload自定義http-request上傳踩坑
el-upload自定義上傳的坑
:http-request 指令的使用
使用該指令, :on-success, :on-error 指令是不會(huì)觸發(fā)的
<el-form-item label="logo" prop="logo"> ? ? ? ? <el-upload ? ? ? ? ? class="upload-demo" ? ? ? ? ? ref="upload" ? ? ? ? ? action="string" ? ? ? ? ? accept="image/jpeg,image/png,image/jpg" ? ? ? ? ? list-type="picture-card" ? ? ? ? ? :before-upload="onBeforeUploadImage" ? ? ? ? ? :http-request="UploadImage" ? ? ? ? ? :on-change="fileChange" ? ? ? ? ? :file-list="fileList" ? ? ? ? >? ? ? ? ? ? <el-button size="small" type="primary">點(diǎn)擊上傳</el-button> ? ? ? ? ? <div slot="tip" class="el-upload__tip">只能上傳jpg/jpeg/png文件,且不超過500kb</div> ? ? ? ? </el-upload> ? ? ? </el-form-item>
說明:
before-upload
上傳文件之前的鉤子action
可以使用普通字符串代替,沒有什么意義,但不建議刪除,因?yàn)槲覀兪褂胔ttp-request 這個(gè)指令http-request
自定義request請(qǐng)求on-change
上傳文件改變時(shí)候觸發(fā)的方法
還可以解決這個(gè)問題 limit設(shè)置為1, 點(diǎn)擊上傳不起效果
data() { ? ? fileList: [], ? ? logo: '', } methods: { ? ? onBeforeUploadImage(file) { ? ? ? const isIMAGE = file.type === 'image/jpeg' || 'image/jpg' || 'image/png' ? ? ? const isLt1M = file.size / 1024 / 1024 < 1 ? ? ? if (!isIMAGE) { ? ? ? ? this.$message.error('上傳文件只能是圖片格式!') ? ? ? } ? ? ? if (!isLt1M) { ? ? ? ? this.$message.error('上傳文件大小不能超過 1MB!') ? ? ? } ? ? ? return isIMAGE && isLt1M ? ? }, ? ? UploadImage(param){ ? ? ? const formData = new FormData() ? ? ? formData.append('file', param.file) ? ? ? UploadImageApi(formData).then(response => { ? ? ? ? ? console.log('上傳圖片成功') ? ? ? ? param.onSuccess() ?// 上傳成功的圖片會(huì)顯示綠色的對(duì)勾 ? ? ? ? // 但是我們上傳成功了圖片, fileList 里面的值卻沒有改變,還好有on-change指令可以使用 ? ? ? }).catch(response => { ? ? ? ? console.log('圖片上傳失敗') ? ? ? ? param.onError() ? ? ? }) ? ? }, ? ? fileChange(file){ ? ? ? this.$refs.upload.clearFiles() //清除文件對(duì)象 ? ? ? this.logo = file.raw // 取出上傳文件的對(duì)象,在其它地方也可以使用 ? ? ? this.fileList = [{name: file.name, url: file.url}] // 重新手動(dòng)賦值filstList, 免得自定義上傳成功了, 而fileList并沒有動(dòng)態(tài)改變, 這樣每次都是上傳一個(gè)對(duì)象 ? ? }, }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue+render+jsx實(shí)現(xiàn)可編輯動(dòng)態(tài)多級(jí)表頭table的實(shí)例代碼
這篇文章主要介紹了vue+render+jsx實(shí)現(xiàn)可編輯動(dòng)態(tài)多級(jí)表頭table的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的工作或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04解決Vue路由導(dǎo)航報(bào)錯(cuò):NavigationDuplicated:?Avoided?redundant?navig
這篇文章主要給大家介紹了關(guān)于解決Vue路由導(dǎo)航報(bào)錯(cuò):NavigationDuplicated:?Avoided?redundant?navigation?to?current?location的相關(guān)資料,這是最近做項(xiàng)目時(shí)候遇到的一個(gè)問題,現(xiàn)將解決辦法分享出來,需要的朋友可以參考下2023-01-01Vue-Router進(jìn)階之滾動(dòng)行為詳解
本篇文章主要介紹了Vue-Router進(jìn)階之滾動(dòng)行為詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09vue中給el-radio添加tooltip并實(shí)現(xiàn)點(diǎn)擊跳轉(zhuǎn)方式
這篇文章主要介紹了vue中給el-radio添加tooltip并實(shí)現(xiàn)點(diǎn)擊跳轉(zhuǎn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04vue項(xiàng)目index.html中使用環(huán)境變量的代碼示例
在Vue3中使用環(huán)境變量的方式與Vue2基本相同,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目index.html中使用環(huán)境變量的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02