国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

vue+element-ui+axios實現圖片上傳

2024-05-06 15:35:43
字體:
來源:轉載
供稿:網友

本文實例為大家分享了vue+element-ui+axios實現圖片上傳的具體代碼,供大家參考,具體內容如下

<!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('圖片太大,請重新選擇');     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('圖片尺寸不對,請重新選擇');     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>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 广南县| 永福县| 枞阳县| 伊宁市| 柳林县| 彭山县| 启东市| 安泽县| 原阳县| 晋江市| 九江县| 江山市| 永安市| 汉中市| 桐城市| 保定市| 崇礼县| 伊川县| 杭锦后旗| 信丰县| 津南区| 钦州市| 林周县| 丽水市| 徐闻县| 蕲春县| 余江县| 仁怀市| 珠海市| 湘潭市| 抚顺市| 西丰县| 苍南县| 通江县| 纳雍县| 布拖县| 淮北市| 昭通市| 广东省| 寻乌县| 梁河县|