VUE UPLOAD 通過(guò)ACTION返回上傳結(jié)果操作
通過(guò)Upload 的action方法 返回不了結(jié)果,可以通過(guò)on-success方法中獲取返回結(jié)果
<Upload accept='.xls, .xlsx' :action='uploadUrl' :on-success='onSuccess' :on-error='handleError' :before-upload='beforeUpload' style='float:right'> <Button type='primary' icon='ios-cloud-upload-outline' >導(dǎo)入</Button> </Upload>-----------------------------------------computed: { uploadUrl() { return baseUrl + '/ImportExcel/'; }//file為ImportExcel方法返回的結(jié)果onSuccess(file){ if(file.code=='1') { this.$Message.error('導(dǎo)入失敗:' + file.msg); return; } },
補(bǔ)充知識(shí):Element-UI中上傳的action地址相對(duì)問(wèn)題
我想要在vue里只出現(xiàn)上傳地址后綴,然后具體的上傳地址,前綴是項(xiàng)目配置里的服務(wù)器地址
1、action直接寫相對(duì)地址
<el-upload :action='/base_data/import_data' :data='uplaodData' name='files' :on-success='uploadSuccess' :on-error='uploadError' accept='xlsx,xls' :show-file-list='false'> <el-button class='btn light small'><i class='icon iconfont icon-piliangdaoru'></i>批量導(dǎo)入</el-button> </el-upload>
這樣的結(jié)果,上傳請(qǐng)求的的前綴都是本地localhost:8080,并不是我想要的相對(duì)服務(wù)器的地址
2、屏蔽掉action地址,我自己寫請(qǐng)求
<el-upload :action='111' //這里隨便寫,反正用不到,但是又必須要寫,無(wú)奈 :before-upload='beforeUpload' :on-success='uploadSuccess' :on-error='uploadError' accept='xlsx,xls' :show-file-list='false'> <el-button class='btn light small'><i class='icon iconfont icon-piliangdaoru'></i>批量導(dǎo)入</el-button> </el-upload>
methods里這么寫
beforeUpload(file){ let fd = new FormData(); fd.append(’files’,file);//傳文件 fd.append(’id’,this.srid);//傳其他參數(shù) axios.post(’/api/up/file’,fd).then(function(res){ alert(’成功’); }) return false //屏蔽了action的默認(rèn)上傳},
這樣的吧但是這樣的我發(fā)過(guò)去的東西老是空的,應(yīng)該是我不太懂FormData()的用法吧,但是我單獨(dú)用FormData()的get方法,都能get到,后來(lái)發(fā)現(xiàn)是因?yàn)槲募幋a問(wèn)題
默認(rèn)的文件編碼application/x-www-form-urlencoded是這個(gè),但是上傳文件需要的是multipart/form-data (這個(gè)格式的請(qǐng)求太好認(rèn), 一長(zhǎng)串有沒(méi)有,里面包括了文件名…),當(dāng)然有時(shí)候也會(huì)是這樣(files: (binary)),都是ok的
啊~,真的要郁悶了,最后還是讓我發(fā)現(xiàn)了一種辦法
那就是?。?!
1、把全局配置的服務(wù)器地址引入
import url from ’@/http/http’
2、在data里定義url:‘’,
3、在create方法里this.url = url;
4、在上傳組件的action上
<el-upload :action='url+this.uploadUrl' //手動(dòng)拼地址 :data='uplaodData' name='files' :on-success='uploadSuccess' :on-error='uploadError' accept='xlsx,xls' :show-file-list='false'> <el-button class='btn light small'><i class='icon iconfont icon-piliangdaoru'></i>批量導(dǎo)入</el-button> </el-upload>
好了,都好了,相對(duì)地址是服務(wù)器地址,上傳文件編碼也是multipart/form-data
以上這篇VUE UPLOAD 通過(guò)ACTION返回上傳結(jié)果操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. idea修改背景顏色樣式的方法2. idea設(shè)置自動(dòng)導(dǎo)入依賴的方法步驟3. IntelliJ IDEA配置Tomcat服務(wù)器的方法4. IntelliJ IDEA調(diào)整字體大小的方法5. python中復(fù)數(shù)的共軛復(fù)數(shù)知識(shí)點(diǎn)總結(jié)6. PHP腳本的10個(gè)技巧(8)7. IntelliJ IDEA設(shè)置背景圖片的方法步驟8. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法9. jsp網(wǎng)頁(yè)實(shí)現(xiàn)貪吃蛇小游戲10. idea自定義快捷鍵的方法步驟
