vue上傳圖片文件的多種實(shí)現(xiàn)方法
最開始百度到各種加headers,最后發(fā)現(xiàn)沒什么用,有大兄弟知道的可以告知一下哦,記個(gè)隨筆
原始input標(biāo)簽form表單上傳
<input type="file" @change="onchangemd"> methods: { onchangemd(e){ console.log(e.target.files)//這個(gè)就是選中文件信息 let formdata = new FormData() Array.from(e.target.files).map(item => { console.log(item) formdata.append("file", item) //將每一個(gè)文件圖片都加進(jìn)formdata }) axios.post("接口地址", formdata).then(res => { console.log(res) }) } }
當(dāng)看到二進(jìn)制提交數(shù)據(jù),就成功了(binary)
這種也是成功的(是二進(jìn)制的展開數(shù)據(jù)。因?yàn)橛行g覽器不顯示binary)
上圖可以看出 傳統(tǒng)上傳文件是以二進(jìn)制的格式formdata格式提交
用elementui自帶的el-upload上傳
<el-upload action="" :on-change="handleelchange" :auto-upload="false" list-type="picture-card"> <i class="el-icon-plus"></i> </el-upload> handleelchange(file, fileList) { console.log(file, fileList) let formdata = new FormData() fileList.map(item => { //fileList本來就是數(shù)組,就不用轉(zhuǎn)為真數(shù)組了 formdata.append("file", item.raw) //將每一個(gè)文件圖片都加進(jìn)formdata }) axios.post("接口地址", formdata).then(res => { console.log(res) }) },
不用設(shè)置請求頭等(直接用FormData格式提交就行),當(dāng)看到formdata數(shù)據(jù)為二進(jìn)制就說明提交正常(binary),同樣有些瀏覽器不顯示binary,以------WebKitFormBoundaryXoZpi2juymcNoW0l 開頭的這種也是提交正常的
注意fileList數(shù)組中的raw才是真實(shí)文件數(shù)據(jù),如果直接用item而不是item.raw,那么會(huì)報(bào)500錯(cuò)誤,并且formdata中的數(shù)據(jù)不是二進(jìn)制而是對象格式
fileList.map(item => { formdata.append(“file”, item) //錯(cuò)誤實(shí)例。要用item.raw })
elementui
elementui實(shí)現(xiàn)一次性上傳多張圖片
注意: 管用思維,點(diǎn)擊提交然后觸發(fā)表單提交事件,然后表單提交事件中發(fā)起請求。
結(jié)果: 上傳每張圖片都需要發(fā)起請求,即會(huì)發(fā)起多次請求
處理: 在submit階段(即this.$refs.xxx.submit() 這步就發(fā)起請求),在提交函數(shù)中僅僅只進(jìn)行圖片獲取
多張圖片上傳最后一個(gè)注意點(diǎn): 多張圖片的這個(gè)file不能寫死,formdata.append(“file”, item) ,寫為每張圖片的指定name,不然會(huì)覆蓋。
<el-upload ref="elupload" action="" multiple :auto-upload="false" :http-request="handleupload" list-type="picture-card"> <i class="el-icon-plus"></i> </el-upload> <button @click="uploadelupload">點(diǎn)擊提交</button> methods:{ uploadelupload() { let formdata = new FormData() this.$refs.elupload.submit(); // 這里是執(zhí)行文件上傳的函數(shù),其實(shí)也就是獲取我們要上傳的文件 this.fileList.forEach(item => { formdata.append("file", item) //將每一個(gè)文件圖片都加進(jìn)formdata }) formdata.append("score", 4) axios.post("接口地址", formdata).then(res => { console.log(res) }) }, handleupload(param) { this.fileList.push(param.file);// 一般情況下是在這里創(chuàng)建FormData對象,但我們需要上傳多個(gè)文件,為避免發(fā)送多次請求,因此在這里只進(jìn)行文件的獲取,param可以拿到文件上傳的所有信息 }, }
elementui提交圖片以及其他數(shù)據(jù)
- 后端讓傳圖片、以及其他數(shù)據(jù)。
- 注意圖片文件等:必須使用formdata傳過去
- 其他數(shù)據(jù)等:用普通方式傳遞
代碼與上述大體相同、這里就只寫axios請求格式 let formdata = new FormData() formdata.append("file", item) //將每一個(gè)文件圖片都 axios({ method: "post", url: "接口地址", data: formdata, params: { score: 4, content: "xxxxx", order_detail_id: 24, token: "xxxx" } }).then(res => { console.log(res) })
總結(jié)
到此這篇關(guān)于vue上傳圖片文件的文章就介紹到這了,更多相關(guān)vue上傳圖片文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3中keep-alive和vue-router的結(jié)合使用方式
這篇文章主要介紹了vue3中keep-alive和vue-router的結(jié)合使用方式,?具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10vue form表單使用resetFields函數(shù)出現(xiàn)的問題
這篇文章主要介紹了vue form表單使用resetFields函數(shù)出現(xiàn)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05vue登錄注冊及token驗(yàn)證實(shí)現(xiàn)代碼
在vue單頁中,我們可以通過監(jiān)控route對象,從中匹配信息去決定是否驗(yàn)證token,然后定義后續(xù)行為。下面通過實(shí)例代碼給大家分享vue登錄注冊及token驗(yàn)證功能,需要的朋友參考下吧2017-12-12vue2 router 動(dòng)態(tài)傳參,多個(gè)參數(shù)的實(shí)例
下面小編就為大家?guī)硪黄獀ue2 router 動(dòng)態(tài)傳參,多個(gè)參數(shù)的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11Vue3如何解決路由緩存問題(響應(yīng)路由參數(shù)的變化)
這篇文章主要介紹了Vue3如何解決路由緩存問題(響應(yīng)路由參數(shù)的變化),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03