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)串有沒有,里面包括了文件名…),當(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. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除2. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究3. ASP基礎(chǔ)知識(shí)VBScript基本元素講解4. React優(yōu)雅的封裝SvgIcon組件示例5. JavaWeb Servlet中url-pattern的使用6. 詳解瀏覽器的緩存機(jī)制7. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)8. jsp中sitemesh修改tagRule技術(shù)分享9. asp(vbscript)中自定義函數(shù)的默認(rèn)參數(shù)實(shí)現(xiàn)代碼10. 輕松學(xué)習(xí)XML教程
