现在谈一下elelmentui中使用Upload 上传通过点击或者拖拽上传文件(图片)

<el-upload
  name="multfile"    //上传的文件字段名
  class="avatar-uploader"
  :action="updateUrl"   //必选参数,上传的地址,即接口地址
  :data="itemForm"   //上传时附带的额外参数
  :before-upload="beforeAvatarUpload"   //上传文件之前的钩子,参数为上传的文件,若返回 false 或者返回 Promise 且被 reject,则停止上传。
  :on-success="handleAvatarSuccess"   //文件上传成功时的钩子函数
  ref="newupload"
>
  <el-button
    slot="trigger"
    size="small"
    icon="el-icon-upload"
    style="margin-top: 20px;"
  >选择上传文件
  </el-button>
  <div slot="tip" class="el-upload__tip">
     只能上传jpg/png文件,且不超过500kb
   </div>
</el-upload>
 
<el-button type="primary" size="small" @click="submitBtn" style="width: 124px;"
>提 交</el-button>
 
 
script中:
  

data() {
  return {
    itemForm: {
    //编辑时数据
      token: sessionStorage.getItem('loginToken'),
      id: 0,
      user_name: '',
      user_nike_name: '',
      user_sex: 1, //默认 1男 0女
      user_phone: '',
      user_email: '',
      head_img: ''
    },
    fd: '', //向服务器进行传递的参数(带有图片formdata)
    updateUrl: this.serverUrl + '/userInfo/update'
  }
},
methods:{
  //成功时保存一下后台给你返回的图片,可以渲染到页面上

  handleAvatarSuccess(res, file) {
    this.itemForm.head_img = URL.createObjectURL(file.raw)
  },
  //上传时,判断文件的类型及大小是否符合规则
  beforeAvatarUpload(file) {
    const isJPG =file.type == 'image/jpeg' || file.type == 'image/png' || file.type == 'image/gif'
    const isLt2M = file.size / 1024 / 1024 < 2
    if (!isJPG) {
      this.$message.warning('上传头像图片只能是 JPG/PNG/GIF 格式!')
      return isJPG
    }
    if (!isLt2M) {
      this.$message.warning('上传头像图片大小不能超过 2MB!')
      return isLt2M
    }
    this.multfileImg = file
    return isJPG && isLt2M
   },
  //点击提交按钮,向服务器传递你要传递的参数,涉及到formData  
  submitBtn() {
    this.$refs.itemForm.validate(valid => {
      if (valid) {
        this.fd = new FormData()
        this.fd.append('token', sessionStorage.getItem('loginToken')) //传其他参数
        this.fd.append('id', this.itemForm.id)
        this.fd.append('user_name', this.itemForm.user_name)
        this.fd.append('user_nike_name', this.itemForm.user_nike_name)
        this.fd.append('user_sex', this.itemForm.user_sex)
        this.fd.append('user_phone', this.itemForm.user_phone)
        this.fd.append('user_email', this.itemForm.user_email)
        if (this.multfileImg != null) {
          this.fd.append('multfile', this.multfileImg)
        }
        api.updateUserInfo(this.fd).then(res => {
          if (res) {
            this.$message({ showClose: true, type: 'success', message: '设置成功' })
            this.initPage()
          }
        })
      } else {
          this.$message({
          showClose: true,
          type: 'error',
          message: '请检查表单信息的正确性'
          })
        return false
      }
    })
  
  }

elementUi--->实现上传图片效果(upload+formData)的更多相关文章

  1. vue ----element-ui 文件上传upload 组件 实现 及其后台

    1.前台 action 不用改 :https://jsonplaceholder.typicode.com/posts/ getFile: 获取文件 data(){ return { file: {} ...

  2. element-ui中上传文件upload

    <el-upload class="upload-demo" name="targetFile" ref="upload" :with ...

  3. vue+Element-ui实现分页效果

    当我们向后台请求大量数据的时候,并要在页面展示出来,请求的数据可能上百条数据或者更多的时候,并不想在一个页面展示,这就需要使用分页功能来去完成了. 1.本次所使用的是vue2.0+element-ui ...

  4. iOS开发 tableView点击下拉扩展 + 内嵌collectionView上传图片效果

    ---恢复内容开始--- //需要的效果 1.设置window的根视图控制器为一个UITableViewController #import "AppDelegate.h"#imp ...

  5. elementUi + express 上传图片

    // 前端代码 <el-upload drag action="http://localhost:4001/article/uploadCoverImage" multipl ...

  6. vue element-ui 组件上传图片 之后 对上传按钮 进行隐藏,删除之后重新显示

    注:如果在当前的 vue 文件里 写了 style 样式,得 去除 scoped [私有属性必须去除,不能保留](这个是重点,不去除不生效), template 部分 <el-upload    ...

  7. vue element-ui 组件上传图片 以及对 图片的 宽高 和 大小 格式等 做出限制

    vue  文件: 自行引用 elemen-ui    <el-upload                         action=" 让后端给你上传地址 "      ...

  8. 如何用elementui去实现图片上传和表单提交,用axios的post方法

    下面是在vue搭建的脚手架项目中的组件component文件夹下面的upload.vue文件中的内容 <!--这个组件主要用来研究upload这个elementui的上传插件组件--> & ...

  9. 改造vue-quill-editor: 结合element-ui上传图片到服务器

    前排提示:现在可以直接使用封装好的插件vue-quill-editor-upload 需求概述 vue-quill-editor是我们再使用vue框架的时候常用的一个富文本编辑器,在进行富文本编辑的时 ...

随机推荐

  1. 内置time模块和random模块

    #time模块#time模块中有三种时间表达方式#时间戳(timestamp):指从1970年1月1号0:0:0开始按秒计算的时间偏移量#元组形式的结构化时间(strut_time):含有9个元素(t ...

  2. 通过关键字Event定义用户自己的事件

    Event 语句 定义用户自定义的事件. 语法[Public] Event procedurename [(arglist)] Event 语句包含下面部分: 部分 描述 Public 可选的.指定该 ...

  3. python笔记(3)---列表

    lst = ["bob","jack","马云","马化腾","王健林"] del lst[1:3] ...

  4. wepy相关

    1.根据官方文档: $ npm install @wepy/cli -g # 全局安装 WePY CLI 工具 $ wepy init standard myproj # 使用 standard 模板 ...

  5. 前端之CSS:属性操作1

    css之操作属性 1.文本 1.文本颜色:color 颜色属性被用来设置文字的颜色. 颜色是通过CSS最经常的指定: 十六进制值 - 如: #FF0000 一个RGB值 - 如: RGB(255,0, ...

  6. git-shell设置代理

    Configure Git to use a proxy ##In Brief You may need to configure a proxy server if you're having tr ...

  7. 【leetcode】622. Design Circular Queue

    题目如下: Design your implementation of the circular queue. The circular queue is a linear data structur ...

  8. BZOJ 3887: [Usaco2015 Jan]Grass Cownoisseur tarjan + spfa

    Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) ...

  9. Activiti流量变量(九)

    1什么是流程变量 流程变量在 activiti 中是一个非常重要的角色,流程运转有时需要靠流程变量,业务系统和 activiti结合时少不了流程变量,流程变量就是 activiti 在管理工作流时根据 ...

  10. EF 查询时,返回其中一张表(省掉一个个写字段的麻烦)

    1.使用partial将需要添加的字段进行扩展 public partial class T_OrderInfo { public string EntName { get; set; } } 2.使 ...