ElementUI浅尝辄止38:Upload 上传

2023-11-01

通过点击或者拖拽上传文件实现上传功能,常见于文件、文件夹或图片上传,使用挺频繁的。需要熟练掌握

1.如何使用?点击上传

通过 slot 你可以传入自定义的上传按钮类型和文字提示。可通过设置limiton-exceed来限制上传文件的个数和定义超出限制时的行为。可通过设置before-remove来阻止文件移除操作。

<el-upload
  class="upload-demo"
  action="https://jsonplaceholder.typicode.com/posts/"
  :on-preview="handlePreview"
  :on-remove="handleRemove"
  :before-remove="beforeRemove"
  multiple
  :limit="3"
  :on-exceed="handleExceed"
  :file-list="fileList">
  <el-button size="small" type="primary">点击上传</el-button>
  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
<script>
  export default {
    data() {
      return {
        fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]
      };
    },
    methods: {
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }?`);
      }
    }
  }
</script>

2.用户头像上传

使用 before-upload 限制用户上传的图片格式和大小。

<el-upload
  class="avatar-uploader"
  action="https://jsonplaceholder.typicode.com/posts/"
  :show-file-list="false"
  :on-success="handleAvatarSuccess"
  :before-upload="beforeAvatarUpload">
  <img v-if="imageUrl" :src="imageUrl" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

<script>
  export default {
    data() {
      return {
        imageUrl: ''
      };
    },
    methods: {
      handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }
    }
  }
</script>

3.照片墙

使用 list-type 属性来设置文件列表的样式。

<el-upload
  action="https://jsonplaceholder.typicode.com/posts/"
  list-type="picture-card"
  :on-preview="handlePictureCardPreview"
  :on-remove="handleRemove">
  <i class="el-icon-plus"></i>
</el-upload>
<el-dialog :visible.sync="dialogVisible">
  <img width="100%" :src="dialogImageUrl" alt="">
</el-dialog>
<script>
  export default {
    data() {
      return {
        dialogImageUrl: '',
        dialogVisible: false
      };
    },
    methods: {
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      }
    }
  }
</script>

4.文件缩略图

使用 scoped-slot 去设置缩略图模版。

<el-upload
  action="#"
  list-type="picture-card"
  :auto-upload="false">
    <i slot="default" class="el-icon-plus"></i>
    <div slot="file" slot-scope="{file}">
      <img
        class="el-upload-list__item-thumbnail"
        :src="file.url" alt=""
      >
      <span class="el-upload-list__item-actions">
        <span
          class="el-upload-list__item-preview"
          @click="handlePictureCardPreview(file)"
        >
          <i class="el-icon-zoom-in"></i>
        </span>
        <span
          v-if="!disabled"
          class="el-upload-list__item-delete"
          @click="handleDownload(file)"
        >
          <i class="el-icon-download"></i>
        </span>
        <span
          v-if="!disabled"
          class="el-upload-list__item-delete"
          @click="handleRemove(file)"
        >
          <i class="el-icon-delete"></i>
        </span>
      </span>
    </div>
</el-upload>
<el-dialog :visible.sync="dialogVisible">
  <img width="100%" :src="dialogImageUrl" alt="">
</el-dialog>
<script>
  export default {
    data() {
      return {
        dialogImageUrl: '',
        dialogVisible: false,
        disabled: false
      };
    },
    methods: {
      handleRemove(file) {
        console.log(file);
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      },
      handleDownload(file) {
        console.log(file);
      }
    }
  }
</script>

5.图片列表缩略图

<el-upload
  class="upload-demo"
  action="https://jsonplaceholder.typicode.com/posts/"
  :on-preview="handlePreview"
  :on-remove="handleRemove"
  :file-list="fileList"
  list-type="picture">
  <el-button size="small" type="primary">点击上传</el-button>
  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
<script>
  export default {
    data() {
      return {
        fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]
      };
    },
    methods: {
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      }
    }
  }
</script>

6.上传文件列表控制

通过 on-change 钩子函数来对列表进行控制

<el-upload
  class="upload-demo"
  action="https://jsonplaceholder.typicode.com/posts/"
  :on-change="handleChange"
  :file-list="fileList">
  <el-button size="small" type="primary">点击上传</el-button>
  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
<script>
  export default {
    data() {
      return {
        fileList: [{
          name: 'food.jpeg',
          url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
        }, {
          name: 'food2.jpeg',
          url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
        }]
      };
    },
    methods: {
      handleChange(file, fileList) {
        this.fileList = fileList.slice(-3);
      }
    }
  }
</script>

7.拖拽上传

<el-upload
  class="upload-demo"
  drag
  action="https://jsonplaceholder.typicode.com/posts/"
  multiple>
  <i class="el-icon-upload"></i>
  <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  <div class="el-upload__tip" slot="tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>

8.手动上传

<el-upload
  class="upload-demo"
  ref="upload"
  action="https://jsonplaceholder.typicode.com/posts/"
  :on-preview="handlePreview"
  :on-remove="handleRemove"
  :file-list="fileList"
  :auto-upload="false">
  <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
  <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
<script>
  export default {
    data() {
      return {
        fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]
      };
    },
    methods: {
      submitUpload() {
        this.$refs.upload.submit();
      },
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      }
    }
  }
</script>

关于上传组件的大致内容就是这些,需要继续深入浅出的,可前往上传组件

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ElementUI浅尝辄止38:Upload 上传 的相关文章

  • Fabric1.4源码解析:链码实例化过程

    之前说完了链码的安装过程 接下来说一下链码的实例化过程好了 再然后是链码的调用过程 其实这几个过程内容已经很相似了 都是涉及到Proposal 不过整体流程还是要说一下的 同样 切入点仍然是fabric peer main go文件中的ma
  • 前端性能优化--预加载技术

    当我们谈到前端的性能时 总是会提到比如合并 压缩 缓存或者在服务器上开启gzip之类的 目的都是为了让页面加载的更快 资源预拉取 prefetch 则是另一种性能优化的技术 通过预拉取可以告诉浏览器用户在未来可能用到哪些资源 Pre fet
  • java开发者工具IDEA自定义设置主题/字体/字号大小

    IDEA自定义设置主题 第一步 点击工具栏上的 File 选项 第二步 选择 Settings 选项 第三步 点击 Appearance Behavior gt Appearance 选项 第四步 点击右侧 Theme 下拉框 选择自己喜欢

随机推荐

  • docker搭建sonar服务

    拉取数据库 docker pull postgres 启动postgres数据库创建sonar用户 docker run name db p 5432 5432 e POSTGRES USER sonar e POSTGRES PASSWO
  • 神经网络二分类问题范例,神经网络解决分类问题

    求运用BP神经网络算法处理分类问题的源程序 例如输入蚊子的翼长和触角长 输出蚊子类型与此类似的源程序 30 这种分类的案例很多 附件里面就有这类案例 主要还是要形成样本 输入输出都做好 进行训练 训练完成后就能满足分类需要 模式识别是对表征
  • 什么是HuggingFace

    一 HuggingFace简介 1 HuggingFace是什么 可以理解为对于AI开发者的GitHub 提供了模型 数据集 文本 图像 音频 视频 类库 比如transformers peft accelerate 教程等 2 为什么需要
  • Yolo v8中的上下文管理器

    from contextlib import contextmanager contextmanager def torch distributed zero first local rank int Decorator to make a
  • 【Pytorch】第 9 章 :Capstone 项目——用 DQN 玩 Flappy Bird

    大家好 我是Sonhhxg 柒 希望你看完之后 能对你有所帮助 不足请指正 共同学习交流 个人主页 Sonhhxg 柒的博客 CSDN博客 欢迎各位 点赞 收藏 留言 系列专栏 机器学习 ML 自然语言处理 NLP 深度学习 DL fore
  • 使用STM32CubeProgrammer烧录STM32芯片

    不使用keil环境烧录STM32时一般需要使用官方的STM32 ST LINK Utility 但今天在使用STM32 ST LINK Utility烧录STM32F401时提示找不到Device ID 查看ST LINK Utility版
  • 解决django运行manage.py runscript命令时报错Try running with a higher verbosity level like: -v2 or -v3

    解决方法 查看命令是否书写正确 比如脚本名称为 my script py 时 python3 manage py runscript my script my script py文件中需要有入口函数 而且此时的 name 属性不是 main
  • css+js网页红包雨效果实现

  • 手把手教你在Android中使用bsdiff实现文件增量更新 (超详细)

    全量更新和增量更新 在Android开发中 版本更新是一个非常常见的需求 目前更新主要分为两种方式 全量更新 增量更新 如下图 分别是酷安和应用宝两个商店的更新页面 可以明显的看到 酷安的更新方式是全量更新 即每次下载全量的新版本文件 当然
  • Ubuntu 18.04 出现GLIBC_2.28 not found的解决方法(亲测有效)

    关于 lib x86 64 linux gnu libc so 6 version GLIBC 2 28 not found 出现报错 建议不要使用源码包去编译并升级 在下文有分享一个使用官方的Debian软件包去升级使用的方法 仅供参考
  • Android开发中的Unable to resolve superclass of L错误解决方法

    这个方法一般是因为引入第三方包造成的 注意android support v4等等也算 解决方法 项目上右键 gt Build Path gt Configure Build Path gt Order and Export里将引入的第三方
  • 头歌:字符串处理

    一 字符串的拼接 将要处理的字符串存放在变量中之后 可直接加 额外 空格需要用 框起来使用 二 字符串的转换 1 获取长度 使用函数len 注意使用字符串变量和直接应用字符串内容的区别 字符串需要加 符号 创建字符串是 输入的字符串也需要加
  • Qt只出现 exited with code 3的错误

    我的这个错误是在debug的时候报错 但是release和profile是可以正常运行的 各种百度以后 还是没有找到解决办法 自己无意中尝试将mingw32改为Mingw64重新运行之后就没问题了 好神奇 虽然解决了bug 但是还是不清楚其
  • 浅谈Http长连接和Keep-Alive以及Tcp的Keepalive

    Keep Alive模式 我们知道Http协议采用 请求 应答 模式 当使用普通模式 即非Keep Alive模式时 每个请求 应答 客户端和服务器都要新建一个连接 完成之后立即断开连接 当使用Keep Alive模式时 Keep Aliv
  • 微服务开发概要设计

    Software environment requirements The software environment includes the software on which the project runs Such as web a
  • NumPy 笔记(超级全!收藏√)

    文章目录 NumPy 教程 NumPy Ndarray 对象 NumPy 数据类型 数据类型对象 dtype NumPy 读取数据 NumPy 数组属性 ndarray ndim ndarray shape ndarray itemsize
  • myslq主从同步和常用命令

    MySQL 5 7 multi source replication 简单的说就是一从多主 从5 7后MySQL开始支持 环境资源 现有4台pc Ip OS alias 172 171 17 144 fedora linux 24 slav
  • centos7使用镜像作为本地yum源

    搭建本地yum源 配置网络 配置网络命令 可使用Tab键辅助 nmcli connection modify ens33 ipv4 method manual ipv4 addresses 192 168 131 161 24 ipv4 g
  • 基于ESP8266实现STM32的远程IAP程序升级(2)

    第二节 串口IAP程序改良 2 1 一种简单直观的方法 2 2 另一种改良版直观方法 2 3 通用IAP升级方法 正点原子的例程仅实现了从IAP至APP程序的跳转 并不能从APP跳转回IAP程序中 但是实际应用中 当我要更新APP程序时 我
  • ElementUI浅尝辄止38:Upload 上传

    通过点击或者拖拽上传文件实现上传功能 常见于文件 文件夹或图片上传 使用挺频繁的 需要熟练掌握 1 如何使用 点击上传 通过 slot 你可以传入自定义的上传按钮类型和文字提示 可通过设置limit和on exceed来限制上传文件的个数和