vue上傳圖片文件的多種實現(xiàn)方法
最開始百度到各種加headers,最后發(fā)現(xiàn)沒什么用,有大兄弟知道的可以告知一下哦,記個隨筆
原始input標(biāo)簽form表單上傳
<input type="file" @change="onchangemd">
methods: {
onchangemd(e){
console.log(e.target.files)//這個就是選中文件信息
let formdata = new FormData()
Array.from(e.target.files).map(item => {
console.log(item)
formdata.append("file", item) //將每一個文件圖片都加進(jìn)formdata
})
axios.post("接口地址", formdata).then(res => { console.log(res) })
}
}
當(dāng)看到二進(jìn)制提交數(shù)據(jù),就成功了(binary)


這種也是成功的(是二進(jìn)制的展開數(shù)據(jù)。因為有些瀏覽器不顯示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) //將每一個文件圖片都加進(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ù)據(jù),如果直接用item而不是item.raw,那么會報500錯誤,并且formdata中的數(shù)據(jù)不是二進(jìn)制而是對象格式
fileList.map(item => {
formdata.append(“file”, item) //錯誤實例。要用item.raw
})
elementui
elementui實現(xiàn)一次性上傳多張圖片
注意: 管用思維,點擊提交然后觸發(fā)表單提交事件,然后表單提交事件中發(fā)起請求。
結(jié)果: 上傳每張圖片都需要發(fā)起請求,即會發(fā)起多次請求
處理: 在submit階段(即this.$refs.xxx.submit() 這步就發(fā)起請求),在提交函數(shù)中僅僅只進(jìn)行圖片獲取
多張圖片上傳最后一個注意點: 多張圖片的這個file不能寫死,formdata.append(“file”, item) ,寫為每張圖片的指定name,不然會覆蓋。
<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">點擊提交</button>
methods:{
uploadelupload() {
let formdata = new FormData()
this.$refs.elupload.submit(); // 這里是執(zhí)行文件上傳的函數(shù),其實也就是獲取我們要上傳的文件
this.fileList.forEach(item => {
formdata.append("file", item) //將每一個文件圖片都加進(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對象,但我們需要上傳多個文件,為避免發(fā)送多次請求,因此在這里只進(jìn)行文件的獲取,param可以拿到文件上傳的所有信息
},
}
elementui提交圖片以及其他數(shù)據(jù)
- 后端讓傳圖片、以及其他數(shù)據(jù)。
- 注意圖片文件等:必須使用formdata傳過去
- 其他數(shù)據(jù)等:用普通方式傳遞
代碼與上述大體相同、這里就只寫axios請求格式
let formdata = new FormData()
formdata.append("file", item) //將每一個文件圖片都
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)文章
vue?perfect-scrollbar(特定框架里使用非該框架定制庫/插件)
這篇文章主要為大家介紹了vue?perfect-scrollbar在特定框架里使用一款并非為該框架定制的庫/插件如何實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2023-05-05
Vue3中使用echarts的簡單七個步驟(易懂,附緊急避坑)
近期在做一個vue3的項目,里面有個圖表需求,因公司之前使用第三方封裝的圖表缺少文檔,就去看了echars的官網(wǎng)文檔,引入原生echars來實現(xiàn),下面這篇文章主要給大家介紹了關(guān)于Vue3中使用echarts的簡單七個步驟,需要的朋友可以參考下2023-01-01

