• ElementUI浅尝辄止38:Upload 上传


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

    1.如何使用?点击上传

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

    1. class="upload-demo"
    2. action="https://jsonplaceholder.typicode.com/posts/"
    3. :on-preview="handlePreview"
    4. :on-remove="handleRemove"
    5. :before-remove="beforeRemove"
    6. multiple
    7. :limit="3"
    8. :on-exceed="handleExceed"
    9. :file-list="fileList">
    10. <el-button size="small" type="primary">点击上传el-button>
    11. <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kbdiv>
    12. <script>
    13. export default {
    14. data() {
    15. return {
    16. 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'}]
    17. };
    18. },
    19. methods: {
    20. handleRemove(file, fileList) {
    21. console.log(file, fileList);
    22. },
    23. handlePreview(file) {
    24. console.log(file);
    25. },
    26. handleExceed(files, fileList) {
    27. this.$message.warning(`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
    28. },
    29. beforeRemove(file, fileList) {
    30. return this.$confirm(`确定移除 ${ file.name }?`);
    31. }
    32. }
    33. }
    34. script>

    2.用户头像上传

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

    1. class="avatar-uploader"
    2. action="https://jsonplaceholder.typicode.com/posts/"
    3. :show-file-list="false"
    4. :on-success="handleAvatarSuccess"
    5. :before-upload="beforeAvatarUpload">
    6. <img v-if="imageUrl" :src="imageUrl" class="avatar">
    7. <i v-else class="el-icon-plus avatar-uploader-icon">i>
    8. el-upload>
    9. <style>
    10. .avatar-uploader .el-upload {
    11. border: 1px dashed #d9d9d9;
    12. border-radius: 6px;
    13. cursor: pointer;
    14. position: relative;
    15. overflow: hidden;
    16. }
    17. .avatar-uploader .el-upload:hover {
    18. border-color: #409EFF;
    19. }
    20. .avatar-uploader-icon {
    21. font-size: 28px;
    22. color: #8c939d;
    23. width: 178px;
    24. height: 178px;
    25. line-height: 178px;
    26. text-align: center;
    27. }
    28. .avatar {
    29. width: 178px;
    30. height: 178px;
    31. display: block;
    32. }
    33. style>
    34. <script>
    35. export default {
    36. data() {
    37. return {
    38. imageUrl: ''
    39. };
    40. },
    41. methods: {
    42. handleAvatarSuccess(res, file) {
    43. this.imageUrl = URL.createObjectURL(file.raw);
    44. },
    45. beforeAvatarUpload(file) {
    46. const isJPG = file.type === 'image/jpeg';
    47. const isLt2M = file.size / 1024 / 1024 < 2;
    48. if (!isJPG) {
    49. this.$message.error('上传头像图片只能是 JPG 格式!');
    50. }
    51. if (!isLt2M) {
    52. this.$message.error('上传头像图片大小不能超过 2MB!');
    53. }
    54. return isJPG && isLt2M;
    55. }
    56. }
    57. }
    58. script>

    3.照片墙

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

    1. action="https://jsonplaceholder.typicode.com/posts/"
    2. list-type="picture-card"
    3. :on-preview="handlePictureCardPreview"
    4. :on-remove="handleRemove">
    5. <i class="el-icon-plus">i>
    6. <el-dialog :visible.sync="dialogVisible">
    7. <img width="100%" :src="dialogImageUrl" alt="">
    8. el-dialog>
    9. <script>
    10. export default {
    11. data() {
    12. return {
    13. dialogImageUrl: '',
    14. dialogVisible: false
    15. };
    16. },
    17. methods: {
    18. handleRemove(file, fileList) {
    19. console.log(file, fileList);
    20. },
    21. handlePictureCardPreview(file) {
    22. this.dialogImageUrl = file.url;
    23. this.dialogVisible = true;
    24. }
    25. }
    26. }
    27. script>

    4.文件缩略图

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

    1. action="#"
    2. list-type="picture-card"
    3. :auto-upload="false">
    4. <i slot="default" class="el-icon-plus">i>
    5. <div slot="file" slot-scope="{file}">
    6. <img
    7. class="el-upload-list__item-thumbnail"
    8. :src="file.url" alt=""
    9. >
    10. <span class="el-upload-list__item-actions">
    11. <span
    12. class="el-upload-list__item-preview"
    13. @click="handlePictureCardPreview(file)"
    14. >
    15. <i class="el-icon-zoom-in">i>
    16. span>
    17. <span
    18. v-if="!disabled"
    19. class="el-upload-list__item-delete"
    20. @click="handleDownload(file)"
    21. >
    22. <i class="el-icon-download">i>
    23. span>
    24. <span
    25. v-if="!disabled"
    26. class="el-upload-list__item-delete"
    27. @click="handleRemove(file)"
    28. >
    29. <i class="el-icon-delete">i>
    30. span>
    31. span>
    32. div>
    33. el-upload>
    34. <el-dialog :visible.sync="dialogVisible">
    35. <img width="100%" :src="dialogImageUrl" alt="">
    36. el-dialog>
    37. <script>
    38. export default {
    39. data() {
    40. return {
    41. dialogImageUrl: '',
    42. dialogVisible: false,
    43. disabled: false
    44. };
    45. },
    46. methods: {
    47. handleRemove(file) {
    48. console.log(file);
    49. },
    50. handlePictureCardPreview(file) {
    51. this.dialogImageUrl = file.url;
    52. this.dialogVisible = true;
    53. },
    54. handleDownload(file) {
    55. console.log(file);
    56. }
    57. }
    58. }
    59. script>

    5.图片列表缩略图

    1. class="upload-demo"
    2. action="https://jsonplaceholder.typicode.com/posts/"
    3. :on-preview="handlePreview"
    4. :on-remove="handleRemove"
    5. :file-list="fileList"
    6. list-type="picture">
    7. <el-button size="small" type="primary">点击上传el-button>
    8. <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kbdiv>
    9. <script>
    10. export default {
    11. data() {
    12. return {
    13. 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'}]
    14. };
    15. },
    16. methods: {
    17. handleRemove(file, fileList) {
    18. console.log(file, fileList);
    19. },
    20. handlePreview(file) {
    21. console.log(file);
    22. }
    23. }
    24. }
    25. script>

    6.上传文件列表控制

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

    1. class="upload-demo"
    2. action="https://jsonplaceholder.typicode.com/posts/"
    3. :on-change="handleChange"
    4. :file-list="fileList">
    5. <el-button size="small" type="primary">点击上传el-button>
    6. <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kbdiv>
    7. <script>
    8. export default {
    9. data() {
    10. return {
    11. fileList: [{
    12. name: 'food.jpeg',
    13. url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
    14. }, {
    15. name: 'food2.jpeg',
    16. url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
    17. }]
    18. };
    19. },
    20. methods: {
    21. handleChange(file, fileList) {
    22. this.fileList = fileList.slice(-3);
    23. }
    24. }
    25. }
    26. script>

    7.拖拽上传

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

    8.手动上传

    1. class="upload-demo"
    2. ref="upload"
    3. action="https://jsonplaceholder.typicode.com/posts/"
    4. :on-preview="handlePreview"
    5. :on-remove="handleRemove"
    6. :file-list="fileList"
    7. :auto-upload="false">
    8. <el-button slot="trigger" size="small" type="primary">选取文件el-button>
    9. <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器el-button>
    10. <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kbdiv>
    11. <script>
    12. export default {
    13. data() {
    14. return {
    15. 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'}]
    16. };
    17. },
    18. methods: {
    19. submitUpload() {
    20. this.$refs.upload.submit();
    21. },
    22. handleRemove(file, fileList) {
    23. console.log(file, fileList);
    24. },
    25. handlePreview(file) {
    26. console.log(file);
    27. }
    28. }
    29. }
    30. script>

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

  • 相关阅读:
    SpringBoot测试类
    <html dir=ltr>是什么意思?
    java网络游戏后台管理系统计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    Oracle 与 MySQL 的区别总结
    使用POI实现操作Excel文件。
    Python:【基础语法】 deque()用法
    CY3.5/CY5/CY5.5/CY7/CY7.5荧光标记壳多糖/壳质/角素Chitin
    CS224W - Colab 5 学习笔记
    Python GDAL读取栅格数据并基于质量评估波段QA对指定数据加以筛选掩膜
    极智嘉(Geek+)官宣重磅合作伙伴,再度赋能仓储自动化解决方案落地
  • 原文地址:https://blog.csdn.net/W2457307263/article/details/132765331