vue項(xiàng)目實(shí)現(xiàn)圖片上傳功能
本文實(shí)例為大家分享了vue實(shí)現(xiàn)圖片上傳功能的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <style> .uploadimg{ width:100px; height:100px; border-radius: 50%; } </style> <body> <div id="app"> <input type="file" @change="upTx" id="txUrl"> <img class="uploadimg" :src="imgsrc"/> </div> </div> </body> <!--<script src="util.js">--> <!--</script>--> <script src="../vue.js"> </script> <script src="../jquery-1.11.1.js"></script> <script src="../axios.js"></script> <script> var vm = new Vue({ el:"#app", data:{ imgsrc:"000", return:{ } }, mounted(){ }, methods:{ upTx() { var _this = this; var r = new FileReader(); var file = document.getElementById("txUrl").files[0]; var beat64Url; if (!/image\/\w+/.test(file.type)) { // this.$message.error("上傳的文件必須為圖片?。?); alert('上傳的文件必須為圖片'); return false; } r.readAsDataURL(file); r.onload = function(e) { beat64Url = this.result; }; var formData = new FormData(); formData.append("upload_img", $("#txUrl")[0].files[0]); axios({ url: "https://mk_api.dthudong.cn/api/file/upImg", method: "POST", headers: { token: '55a31e89d7dd8b80282cba7d844d1c28' }, data: formData }) .then(res => { console.log(res.data); if (res.data.code == 0) { vm.imgsrc =res.data.data.src; console.log(vm.imgsrc) } else { _this.$message({ message: res.data.msg, type: "alert" }); } }) .catch(function(error) { }); }, } }) </script> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue使用vux-ui自定義表單驗(yàn)證遇到的問(wèn)題及解決方法
這篇文章主要介紹了Vue使用vux-ui自定義表單驗(yàn)證遇到的問(wèn)題及解決方法,需要的朋友可以參考下2018-05-05vue 導(dǎo)航內(nèi)容設(shè)置選中狀態(tài)樣式的例子
今天小編就為大家分享一篇vue 導(dǎo)航內(nèi)容設(shè)置選中狀態(tài)樣式的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11Vue Element前端應(yīng)用開發(fā)之根據(jù)ABP后端接口實(shí)現(xiàn)前端展示
本篇著重介紹基于ABP后端接口信息,實(shí)現(xiàn)對(duì)前端界面的開發(fā)工作。2021-05-05vue中axios處理http發(fā)送請(qǐng)求的示例(Post和get)
本篇文章主要介紹了vue中axios處理http請(qǐng)求的示例(Post和get),這里整理了詳細(xì)的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Vue中router.beforeEach與beforeRouteEnter的區(qū)別及說(shuō)明
這篇文章主要介紹了Vue中router.beforeEach與beforeRouteEnter的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Vue發(fā)布訂閱模式實(shí)現(xiàn)過(guò)程圖解
這篇文章主要介紹了Vue發(fā)布訂閱模式實(shí)現(xiàn)過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04vue?實(shí)現(xiàn)列表跳轉(zhuǎn)至詳情且能添加至購(gòu)物車功能
列表頁(yè)面顯示數(shù)據(jù),點(diǎn)擊跳轉(zhuǎn)到對(duì)應(yīng)的詳情頁(yè),詳情頁(yè)可以添加并跳轉(zhuǎn)到購(gòu)物車,購(gòu)物車具有常見(jiàn)功能,這篇文章主要介紹了vue?實(shí)現(xiàn)列表跳轉(zhuǎn)至詳情且能添加至購(gòu)物車,需要的朋友可以參考下2022-10-10