vue實(shí)現(xiàn)的上傳圖片到數(shù)據(jù)庫并顯示到頁面功能示例
本文實(shí)例講述了vue實(shí)現(xiàn)的上傳圖片到數(shù)據(jù)庫并顯示到頁面功能。分享給大家供大家參考,具體如下:
1、點(diǎn)擊上傳圖片,彈出選擇圖片選項(xiàng)框。
頁面代碼:
<div class="form-signin-heading" id="btnUpload" @change="upload">上傳圖片</div> <input type="file" name="avatar" id="avatar" multiple="multiple" @change="upload"> <img :src="'http://localhost:8888'+item.photos_url" alt=""/>
由于我們要設(shè)置上傳圖片的樣式,所以把input隱藏,并要做如下操作把input的點(diǎn)擊事件給div框:
mounted: function () { var upload = document.getElementById("btnUpload"); var avatar = document.getElementById("avatar"); upload.onclick =function(){ avatar.click(); //注意IE的兼容性 }; }
2、在api接口的controller層加入兩個(gè)文件,命名自己定,如:
upFile.js
let multer=require('multer'); let storage = multer.diskStorage({ //設(shè)置上傳后文件路徑,uploads文件夾會(huì)自動(dòng)創(chuàng)建。 destination: function (req, file, cb) { cb(null, './public/uploads') }, //給上傳文件重命名,獲取添加后綴名 filename: function (req, file, cb) { let fileFormat = (file.originalname).split("."); cb(null, file.fieldname + '-' + Date.now() + "." + fileFormat[fileFormat.length - 1]); } }); //添加配置文件到multer對(duì)象。 let upload = multer({ storage: storage }); module.exports = upload;
upFileController.js
var muilter = require('./upFile.js'); //multer有single()中的名稱必須是表單上傳字段的name名稱。 var upload=muilter.single('file'); function dataInput(req, res) { upload(req, res, function (err) { //添加錯(cuò)誤處理 if (err) { return console.log(err); } //文件信息在req.file或者req.files中顯示。 let photoPath = req.file.path; photoPath = photoPath.replace(/public/,"");//將文件路徑中的public\去掉,否則會(huì)和靜態(tài)資源配置沖突 //將photoPath存入數(shù)據(jù)庫即可 console.log(photoPath); res.send(photoPath); }); } module.exports = { dataInput };
3、在頁面中將圖片的地址存到數(shù)據(jù)庫
upload: function (e) { var that = this; let formData = new window.FormData(); let file = e.target.files[0]; formData.append('file',file);//通過append向form對(duì)象添加數(shù)據(jù) //利用split切割,拿到上傳文件的格式 var src = file.name, formart = src.split(".")[1]; //使用if判斷上傳文件格式是否符合 if (formart == "jpg" || formart == "png" || formart == "docx" || formart == "txt" || formart == "ppt" || formart == "xlsx" || formart == "zip" || formart == "rar" || formart == "doc") { //只有滿足以上格式時(shí),才會(huì)觸發(fā)ajax請(qǐng)求 this.$axios.post(this.$api.personalCenter.upFile,formData).then(function (res) { that.upFileData = res.data; }).then(function (res) { var params = { photos_url: that.upFileData, photo_des: '' }; // console.log(params.photos_url,'photos_url') that.$axios.post(that.$api.personalCenter.wallAdd,qs.stringify(params)).then(function (res) { console.log(res.data); that.$options.methods.imgList.bind(that)(); }).catch(function (err) { console.log(err); console.log("請(qǐng)求出錯(cuò)"); }) }) } else { alert("文件格式不支持上傳"); } }
希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。
相關(guān)文章
vue項(xiàng)目打包之開發(fā)環(huán)境和部署環(huán)境的實(shí)現(xiàn)
這篇文章主要介紹了vue項(xiàng)目打包之開發(fā)環(huán)境和部署環(huán)境的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Vue.js中Line第三方登錄api的實(shí)現(xiàn)代碼
這篇文章主要介紹了Vue.js中Line第三方登錄api實(shí)現(xiàn)代碼,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06vue.js el-tooltip根據(jù)文字長(zhǎng)度控制是否提示toolTip問題
這篇文章主要介紹了vue.js el-tooltip根據(jù)文字長(zhǎng)度控制是否提示toolTip問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02一次Vue中computed沒有觸發(fā)的原因以及排查經(jīng)歷
這篇文章主要介紹了一次Vue中computed沒有觸發(fā)的原因以及排查經(jīng)歷,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11vue使用echarts實(shí)現(xiàn)三維圖表繪制
這篇文章主要為大家詳細(xì)介紹了vue如何在項(xiàng)目中使用echarts實(shí)現(xiàn)三維圖表的繪制,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2023-08-08vue項(xiàng)目使用$router.go(-1)返回時(shí)刷新原來的界面操作
這篇文章主要介紹了vue項(xiàng)目使用$router.go(-1)返回時(shí)刷新原來的界面操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07highCharts提示框中顯示當(dāng)前時(shí)間的方法
今天小編就為大家分享一篇關(guān)于highCharts提示框中顯示當(dāng)前時(shí)間的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01vue解決花括號(hào)數(shù)據(jù)綁定不成功的問題
今天小編就為大家分享一篇vue解決花括號(hào)數(shù)據(jù)綁定不成功的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10vue實(shí)現(xiàn)點(diǎn)擊按鈕切換背景顏色的示例代碼
這篇文章主要介紹了用vue簡(jiǎn)單的實(shí)現(xiàn)點(diǎn)擊按鈕切換背景顏色,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06