element-ui配合node實(shí)現(xiàn)自定義上傳文件方式
element-ui配合node實(shí)現(xiàn)自定義上傳文件
某些情況下,使用element-ui的upload組件默認(rèn)上傳無(wú)法滿足我們的需求,so~今天主要介紹如何使用element-ui實(shí)現(xiàn)自定義上傳,以及后端如何接收上傳的文件信息和其他信息,根據(jù)element-ui文檔,http-request可以自定義上傳方法,會(huì)覆蓋掉默認(rèn)的上傳。
首先我們來(lái)看前端代碼:
<template> ? <div style="margin-top:100px"> ? ? <el-form :model="form"> ? ? ? <el-form-item label="姓名" label-width="50px"> ? ? ? ? <el-input v-model="form.name"></el-input> ? ? ? </el-form-item> ? ? ? <el-form-item label="年齡" label-width="50px"> ? ? ? ? <el-input v-model="form.age"></el-input> ? ? ? </el-form-item> ? ? ? <el-form-item label="文件" label-width="50px"> ? ? ? ? <el-upload? ? ? ? ? ? class="upload-demo"? ? ? ? ? ? :auto-upload="false"? ? ? ? ? ? :limit="1"? ? ? ? ? ? ref="upload" ? ? ? ? ? :http-request="upload" ? ? ? ? ? multiple> ? ? ? ? ? <!-- :http-request="upload" --> ? ? ? ? ? <el-button size="small" type="primary">點(diǎn)擊上傳</el-button> ? ? ? ? ? <div slot="tip" class="el-upload__tip">只能上傳pdf文件</div> ? ? ? ? </el-upload> ? ? ? </el-form-item> ? ? </el-form> ? ?? ? ? <el-button type="primary" @click="upload">確定</el-button> ? </div> </template>
<script> ? export default{ ? ? name: 'upload', ? ? data() { ? ? ? return { ? ? ? ? form: { ? ? ? ? ? name: '', ? ? ? ? ? age: '' ? ? ? ? } ? ? ? } ? ? }, ? ? methods: { ? ? ? upload() { ? ? ? ? const formData = new FormData(); ? ? ? ? const file = this.$refs.upload.uploadFiles[0]; ? ? ? ? const headerConfig = { headers: { 'Content-Type': 'multipart/form-data' } }; ? ? ? ? if (!file) { // 若未選擇文件 ? ? ? ? ? alert('請(qǐng)選擇文件'); ? ? ? ? ? return; ? ? ? ? } ? ? ? ? formData.append('file', file.raw); ? ? ? ? formData.append('name', this.name); ? ? ? ? formData.append('age', this.age); ? ? ? ? this.$http.post('/api/upload', formData, headerConfig).then(res => { ? ? ? ? ? console.log(res); ? ? ? ? }) ? ? ? } ? ? } ? } </script>
由于是上傳文件,所以我們需要配置下axios請(qǐng)求header的Content-Type為'multipart/form-data',this.$refs.upload.uploadFiles[0].raw即為我們選擇的文件,name和age則為其他要傳的信息。
接著,我們來(lái)看后端如何獲取,這里以node為例:
我們需要安裝multer中間件,npm install multer -S即可,接下來(lái)我們來(lái)看如何使用multer。
首先引入multer:
var express = require('express'); var multer = require('multer') const upload = multer({ dest: 'uploads/' }); ? var app = express(); app.use(upload.single('file')); // ? app.post('/api/upload', (req, res)=>{ ? console.log(req.body);//獲取到的age和name ? console.log(req.file);//獲取到的文件 ? ? //做些其他事情 })
整個(gè)過(guò)程下來(lái),就完成了圖片的上傳,非常簡(jiǎn)單。
關(guān)于multer的用法:https://www.npmjs.com/package/multer
自定義elementui上傳文件及攜帶參數(shù)
elementui提供了上傳文件的ui,可以比較方便,我們需要它來(lái)完成自定義上傳文件以及攜帶參數(shù)需要改寫(xiě)其中的一些方法來(lái)實(shí)現(xiàn)。
下面是一個(gè)簡(jiǎn)單的上傳標(biāo)簽
? ? <el-upload ? ? ? ? ref="upload" ? ? ? ? style="display: inline" ? ? ? ? drag ? ? ? ? action="" ? ? ? ? :before-upload="beforeUploadHandle" ? ? ? ? :http-request="handleUploadForm" ? ? ? ? :auto-upload="false" ? ? ? ? multiple ? ? ? ? :limit="1" ? ? ? ? :on-exceed="handleExceed"> ? ? ? <el-link icon="el-icon-paperclip" type="primary">添加需要上傳的文件</el-link> ? ? </el-upload>
不自動(dòng)上傳
:auto-upload=“false”
限制上傳文件個(gè)數(shù)
:limit=“1”
上傳之前的校驗(yàn)
我們需要實(shí)現(xiàn)這個(gè)方法,來(lái)檢測(cè)上傳的合法性
:before-upload=“beforeUploadHandle”
上傳時(shí)需要攜帶參數(shù)
需要上傳的數(shù)據(jù)文件以及參數(shù)在這里來(lái)處理添加
:http-request=“handleUploadForm”
檢測(cè)添加文件是否超過(guò)限制
:on-exceed=“handleExceed”
手動(dòng)上傳
其中的upload對(duì)應(yīng)于上面的ref=“upload”,我們只需要給按鈕實(shí)現(xiàn)一個(gè)方法執(zhí)行此語(yǔ)句便可以實(shí)現(xiàn)自己點(diǎn)擊上傳
this.$refs.upload.submit();
具體實(shí)現(xiàn)
上傳限制以及上傳之前的校驗(yàn)不贅述,這里給出一個(gè)簡(jiǎn)單的demo。上傳限制實(shí)現(xiàn)類似,只是給出一些提示信息,file便是我們上傳的文件。
beforeUploadHandle(file) { if ( file.type !== "image/png" && file.type !== "image/jpeg" ) { this.$message.error("只支持.jpg、.jpeg、.jpe、.png文件!"); return false; } },
攜帶參數(shù)
handleUploadForm(param) { console.log(this.pid) let thisInfo = this let formData = new FormData() // 在formData中加入我們需要的參數(shù) formData.append('file', param.file) formData.append('id', this.pid) // 向后端發(fā)送數(shù)據(jù) thisInfo.$axios.post('api/user/update_Info/', formData).then((res) => { if (res.status === 200) { thisInfo.$message.success('修改信息成功') } else { thisInfo.$message.success('修改信息失敗') } thisInfo.formFileList = [] thisInfo.uploadFormFileList = [] }) }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue登錄路由權(quán)限管理的項(xiàng)目實(shí)踐
在開(kāi)發(fā)Web應(yīng)用程序時(shí),常常需要進(jìn)行登錄驗(yàn)證和權(quán)限管理,本文主要介紹了vue登錄路由權(quán)限管理的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Vue?+?Element?實(shí)現(xiàn)按鈕指定間隔時(shí)間點(diǎn)擊思路詳解
這篇文章主要介紹了Vue?+?Element?實(shí)現(xiàn)按鈕指定間隔時(shí)間點(diǎn)擊,實(shí)現(xiàn)思路大概是通過(guò)加一個(gè)本地緩存的時(shí)間戳,通過(guò)時(shí)間戳計(jì)算指定時(shí)間內(nèi)不能點(diǎn)擊按鈕,具體實(shí)現(xiàn)代碼跟隨小編一起看看吧2023-12-12vue的路由動(dòng)畫(huà)切換頁(yè)面無(wú)法讀取meta值的bug記錄
這篇文章主要介紹了vue的路由動(dòng)畫(huà)切換頁(yè)面無(wú)法讀取meta值的bug記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05vue?懸浮窗且?guī)ё詣?dòng)吸附功能實(shí)現(xiàn)demo
這篇文章主要為大家介紹了vue?懸浮窗且?guī)ё詣?dòng)吸附功能實(shí)現(xiàn)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06關(guān)于vue-tree-chart簡(jiǎn)單的使用
這篇文章主要介紹了關(guān)于vue-tree-chart簡(jiǎn)單的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue項(xiàng)目調(diào)試的三種方法總結(jié)
這篇文章主要給大家總結(jié)介紹了關(guān)于vue項(xiàng)目調(diào)試的三種方法,大家可以根據(jù)需要選擇調(diào)試方法,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09關(guān)于怎么在vue項(xiàng)目里寫(xiě)react詳情
本篇文章是在vue項(xiàng)目里寫(xiě)tsx的一篇介紹。其實(shí)vue里面寫(xiě)jsx也挺有意思的,接下來(lái)小編九給大家詳細(xì)介紹吧,感興趣的小伙伴請(qǐng)參考下面的文章內(nèi)容2021-09-09