vue+element-ui+axios實(shí)現(xiàn)圖片上傳
本文實(shí)例為大家分享了vue+element-ui+axios實(shí)現(xiàn)圖片上傳的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>Examples</title> <meta name="description" content=""> <meta name="keywords" content=""> <!-- 引入vue --> <script src="https://cdn.jsdelivr.net/npm/vue"></script> <!-- 引入axios --> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> <!-- 引入樣式 --> <link rel="stylesheet" href=https://unpkg.com/element-ui/lib/theme-chalk/index.css > <!-- 引入組件庫 --> <script src="https://unpkg.com/element-ui/lib/index.js"></script> </head> <body> <div id="app"> <el-upload :action="posterUrl" :before-upload="beforeUpload" :http-request="(params)=>uploadImage(params)" :on-remove="(file, fileList)=>removeImage(file, fileList)" list-type="picture" accept="image/*" > <el-button size="small">選擇圖片</el-button> </el-upload> </div> <script type="text/javascript"> new Vue({ el: '#app', data: { posterUrl: '', imgUrls: [], imgWidth: '320', imgHeight: '400', }, methods: { beforeUpload(file) { let _this = this; let _checkSize = false; //是否需要指定上傳圖片的尺寸 if(file.size > 1024*500) { //大小超過500kb _this.$message.error('圖片太大,請(qǐng)重新選擇'); return false; } const isSize = new Promise((resolve, reject)=>{ let _URL = window.URL || window.webkitURL; let img = new Image(); img.onload = function () { if(!_checkSize || (_checkSize && img.width==_this.imgWidth && img.height==_this.imgHeight)) { resolve(); } else { reject(); } } img.src = _URL.createObjectURL(file); }).then(()=>{ return file; }, ()=>{ _this.$message.error('圖片尺寸不對(duì),請(qǐng)重新選擇'); return Promise.reject(); }); return isSize; }, uploadImage(params) { console.log(params); let uploadData = new FormData(); uploadData.append('file', params.file); let config = { headers: { 'Content-Type': 'multipart/form-data' } }; this.uploadPoster('homed'+new Date().getTime()+'/'+params.file.name, uploadData, config) .then(res=>{ if(res.status == 200) { params.onSuccess(); this.imgUrls.push({name:params.file.name, url:res.data.url}); console.log(this.imgUrls); } }).catch(error=>{ params.onError(); this.$message.error('上傳失敗'); }); }, removeImage(file, fileList) { console.log(fileList); }, uploadPoster(url, obj, config) { let poster_upload_path = "http://xxxxxxxxxxxx/httpdocsup/poster/news/"; return axios.post(poster_upload_path+url, obj, config); } } }) </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
elementui之封裝下載模板和導(dǎo)入文件組件方式
這篇文章主要介紹了關(guān)于elementui之封裝下載模板和導(dǎo)入文件組件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
Vue3 組合式函數(shù)Composable最佳實(shí)戰(zhàn)
這篇文章主要為大家介紹了Vue3 組合式函數(shù)Composable最佳實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
ssm+vue前后端分離框架整合實(shí)現(xiàn)(附源碼)
這篇文章主要介紹了ssm+vue前后端分離框架整合實(shí)現(xiàn)(附源碼),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
vue項(xiàng)目中引入vue-datepicker插件的詳解
這篇文章主要介紹了vue項(xiàng)目中引入vue-datepicker插件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Vue3項(xiàng)目引入阿里iconfont圖標(biāo)與字體及使用教程
Iconfont國內(nèi)功能很強(qiáng)大且圖標(biāo)內(nèi)容很豐富的矢量圖標(biāo)庫,提供矢量圖標(biāo)下載、在線存儲(chǔ)、格式轉(zhuǎn)換等功能,下面這篇文章主要給大家介紹了關(guān)于Vue3項(xiàng)目引入阿里iconfont圖標(biāo)與字體及使用教程,需要的朋友可以參考下2023-05-05
前端使用vue點(diǎn)擊上傳文件傳送給后端并進(jìn)行文件接收的方法
這篇文章主要介紹了如何在前端和后端實(shí)現(xiàn)文件傳輸,前端使用Vue.js發(fā)送文件,后端使用Java接收文件并處理,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01

