vue上傳圖片文件的多種實(shí)現(xiàn)方法
最開始百度到各種加headers,最后發(fā)現(xiàn)沒(méi)什么用,有大兄弟知道的可以告知一下哦,記個(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本來(lái)就是數(shù)組,就不用轉(zhuǎn)為真數(shù)組了
formdata.append("file", item.raw) //將每一個(gè)文件圖片都加進(jìn)formdata
})
axios.post("接口地址", formdata).then(res => { console.log(res) })
},
不用設(shè)置請(qǐng)求頭等(直接用FormData格式提交就行),當(dāng)看到formdata數(shù)據(jù)為二進(jìn)制就說(shuō)明提交正常(binary),同樣有些瀏覽器不顯示binary,以------WebKitFormBoundaryXoZpi2juymcNoW0l 開頭的這種也是提交正常的

注意fileList數(shù)組中的raw才是真實(shí)文件數(shù)據(jù),如果直接用item而不是item.raw,那么會(huì)報(bào)500錯(cuò)誤,并且formdata中的數(shù)據(jù)不是二進(jìn)制而是對(duì)象格式
fileList.map(item => {
formdata.append(“file”, item) //錯(cuò)誤實(shí)例。要用item.raw
})
elementui
elementui實(shí)現(xiàn)一次性上傳多張圖片
注意: 管用思維,點(diǎn)擊提交然后觸發(fā)表單提交事件,然后表單提交事件中發(fā)起請(qǐng)求。
結(jié)果: 上傳每張圖片都需要發(fā)起請(qǐng)求,即會(huì)發(fā)起多次請(qǐng)求
處理: 在submit階段(即this.$refs.xxx.submit() 這步就發(fā)起請(qǐng)求),在提交函數(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對(duì)象,但我們需要上傳多個(gè)文件,為避免發(fā)送多次請(qǐng)求,因此在這里只進(jìn)行文件的獲取,param可以拿到文件上傳的所有信息
},
}
elementui提交圖片以及其他數(shù)據(jù)
- 后端讓傳圖片、以及其他數(shù)據(jù)。
- 注意圖片文件等:必須使用formdata傳過(guò)去
- 其他數(shù)據(jù)等:用普通方式傳遞
代碼與上述大體相同、這里就只寫axios請(qǐng)求格式
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
導(dǎo)致VUE頁(yè)面不刷新的問(wèn)題分析及解決方法
由于 Vue 會(huì)在初始化實(shí)例時(shí)對(duì) property 執(zhí)行 getter/setter 轉(zhuǎn)化,所以 property 必須在 data 對(duì)象上存在才能讓 Vue 將它轉(zhuǎn)換為響應(yīng)式的,這篇文章主要介紹了導(dǎo)致VUE頁(yè)面不刷新的問(wèn)題分析及解決方法,需要的朋友可以參考下2024-04-04
vue3中keep-alive和vue-router的結(jié)合使用方式
這篇文章主要介紹了vue3中keep-alive和vue-router的結(jié)合使用方式,?具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
vue form表單使用resetFields函數(shù)出現(xiàn)的問(wèn)題
這篇文章主要介紹了vue form表單使用resetFields函數(shù)出現(xiàn)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
vue登錄注冊(cè)及token驗(yàn)證實(shí)現(xiàn)代碼
在vue單頁(yè)中,我們可以通過(guò)監(jiān)控route對(duì)象,從中匹配信息去決定是否驗(yàn)證token,然后定義后續(xù)行為。下面通過(guò)實(shí)例代碼給大家分享vue登錄注冊(cè)及token驗(yàn)證功能,需要的朋友參考下吧2017-12-12
vue2 router 動(dòng)態(tài)傳參,多個(gè)參數(shù)的實(shí)例
下面小編就為大家?guī)?lái)一篇vue2 router 動(dòng)態(tài)傳參,多個(gè)參數(shù)的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Vue3如何解決路由緩存問(wèn)題(響應(yīng)路由參數(shù)的變化)
這篇文章主要介紹了Vue3如何解決路由緩存問(wèn)題(響應(yīng)路由參數(shù)的變化),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03

