VUE UPLOAD 通過ACTION返回上傳結(jié)果操作
通過Upload 的action方法 返回不了結(jié)果,可以通過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地址相對問題
我想要在vue里只出現(xiàn)上傳地址后綴,然后具體的上傳地址,前綴是項(xiàng)目配置里的服務(wù)器地址
1、action直接寫相對地址
<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é)果,上傳請求的的前綴都是本地localhost:8080,并不是我想要的相對服務(wù)器的地址
2、屏蔽掉action地址,我自己寫請求
<el-upload :action='111' //這里隨便寫,反正用不到,但是又必須要寫,無奈 :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ā)過去的東西老是空的,應(yīng)該是我不太懂FormData()的用法吧,但是我單獨(dú)用FormData()的get方法,都能get到,后來發(fā)現(xiàn)是因?yàn)槲募幋a問題
默認(rèn)的文件編碼application/x-www-form-urlencoded是這個(gè),但是上傳文件需要的是multipart/form-data (這個(gè)格式的請求太好認(rèn), 一長串有沒有,里面包括了文件名…),當(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>
好了,都好了,相對地址是服務(wù)器地址,上傳文件編碼也是multipart/form-data
以上這篇VUE UPLOAD 通過ACTION返回上傳結(jié)果操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向2. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明3. CSS hack用法案例詳解4. PHP設(shè)計(jì)模式中工廠模式深入詳解5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. ASP+ajax實(shí)現(xiàn)頂一下、踩一下同支持與反對的實(shí)現(xiàn)代碼7. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法8. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過程(親測可用)9. asp中response.write("中文")或者js中文亂碼問題10. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析
