温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

vue+element-ui+axios如何实现图片上传

发布时间:2021-05-24 13:44:27 来源:亿速云 阅读:316 作者:小新 栏目:web开发

这篇文章主要介绍了vue+element-ui+axios如何实现图片上传,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

本文实例为大家分享了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>

Vue的优点

Vue具体轻量级框架、简单易学、双向数据绑定、组件化、数据和结构的分离、虚拟DOM、运行速度快等优势,Vue中页面使用的是局部刷新,不用每次跳转页面都要请求所有数据和dom,可以大大提升访问速度和用户体验。

感谢你能够认真阅读完这篇文章,希望小编分享的“vue+element-ui+axios如何实现图片上传”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI