1.前言

之前在做项目的时候,需要实现一个文件上传组件并且需要有文件上传进度条,现将之前的实现过程简单记录一下,希望可以帮助到有需要的人。

项目用的是Vue框架,UI库使用的是element UI,前后端交互请求使用的是Vue官方推荐的axios。其中,UI方面主要使用了element UI库中的Upload文件上传组件、Progress 进度条组件。

2.文件上传

文件上传功能使用element UI库中的Upload文件上传组件实现,代码如下:

  1. <div class="uploadfile">
  2. <el-upload
  3. ref="upload"
  4. class="upload-demo"
  5. :before-upload="beforeUpload"
  6. drag
  7. :auto-upload="false"
  8. :on-exceed="handleExceed"
  9. >
  10. <i class="el-icon-upload"></i>
  11. <div class="el-upload__text">将文件拖到此处,或<em>点击选择文件</em></div>
  12. </el-upload>
  13. <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传</el-button>
  14. </div>

当点击上传按钮,会触发submitUpload函数,同时该函数也会触发beforeUpload函数:

  1. beforeUpload(file){
  2. let fd = new FormData();
  3. fd.append('file', file);
  4. let config = {
  5. onUploadProgress: progressEvent => {
  6. let complete = (progressEvent.loaded / progressEvent.total ).toFixed(2) * 100 ;
  7. this.percentage = complete;
  8. if (this.percentage >= 100){
  9. this.dialogVisible = true
  10. }
  11. },
  12. headers: {
  13. 'Content-Type': 'multipart/form-data'
  14. }
  15. };
  16. this.$axios.post(this.url,fd,config)
  17. .then((res)=>{
  18. })
  19. .catch((err)=>{
  20. })
  21. },
  22. submitUpload(){
  23. this.loading = true;
  24. this.tips = '正在上传中。。。';
  25. this.$refs.upload.submit();
  26. },

3.进度条

当点击上传后,整个页面被遮罩层遮挡,并显示上传进度:

  1. <!--遮罩层-->
  2. <div class="loading" v-if="loading" >
  3. <h4 class="tips">{{tips}}</h4>
  4. <!--进度条-->
  5. <el-progress type="line" :percentage="percentage" class="progress" :show-text="true"></el-progress>
  6. </div>

进度条关键代码:

进度条的实现主要依靠axios中提供的onUploadProgress函数,该函数提供了文件已上传部分的大小progressEvent.loaded和文件总大小progressEvent.total,利用这两个数据我们就可以计算出已经上传文件的进度。

  1. beforeUpload(file){
  2. let fd = new FormData();
  3. fd.append('file', file);
  4. let config = {
  5. onUploadProgress: progressEvent => {
  6. //progressEvent.loaded:已上传文件大小
  7. //progressEvent.total:被上传文件的总大小
  8. let complete = (progressEvent.loaded / progressEvent.total ).toFixed(2) * 100 ;
  9. this.percentage = complete;
  10. if (this.percentage >= 100){
  11. this.dialogVisible = true
  12. }
  13. },
  14. headers: {
  15. 'Content-Type': 'multipart/form-data'
  16. }
  17. };
  18. this.$axios.post(this.url,fd,config)
  19. .then((res)=>{
  20. })
  21. .catch((err)=>{
  22. })
  23. },

4.全部代码

封装好组件后,我们只需在父组件中调用该组件并传入文件上传到的目的url即可。

  1. <UploadFile :url="/test/"/>

以下是该组件UploadFile.vue的全部代码:

  1. <template>
  2. <div>
  3. <!--文件上传入口-->
  4. <div class="uploadfile">
  5. <el-upload
  6. ref="upload"
  7. class="upload-demo"
  8. :before-upload="beforeUpload"
  9. drag
  10. :auto-upload="false"
  11. :on-exceed="handleExceed"
  12. >
  13. <i class="el-icon-upload"></i>
  14. <div class="el-upload__text">将文件拖到此处,或<em>点击选择文件</em></div>
  15. </el-upload>
  16. <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传</el-button>
  17. </div>
  18. <!--遮罩层-->
  19. <div class="loading" v-if="loading" >
  20. <h4 class="tips">{{tips}}</h4>
  21. <!--进度条-->
  22. <el-progress type="line" :percentage="percentage" class="progress" :show-text="true"></el-progress>
  23. </div>
  24. <!--上传完成提示对话框-->
  25. <el-dialog
  26. title="提示"
  27. :visible="dialogVisible"
  28. width="30%"
  29. :modal-append-to-body='false'
  30. >
  31. <span>文件上传成功</span>
  32. <span slot="footer" class="dialog-footer">
  33. <el-button type="primary" @click="ensure">确 定</el-button>
  34. </span>
  35. </el-dialog>
  36. </div>
  37. </template>
  38. <script>
  39. import Vue from 'vue'
  40. import {Upload,Button,Progress,Dialog} from 'element-ui';
  41. Vue.use(Upload);
  42. Vue.use(Button);
  43. Vue.use(Progress);
  44. Vue.use(Dialog);
  45. export default {
  46. name: "UploadFile",
  47. data(){
  48. return {
  49. loading:false,
  50. percentage:0,
  51. tips:'',
  52. dialogVisible:false
  53. }
  54. },
  55. props:['url'],
  56. methods:{
  57. beforeUpload(file){
  58. let fd = new FormData();
  59. fd.append('file', file);
  60. let config = {
  61. onUploadProgress: progressEvent => {
  62. //progressEvent.loaded:已上传文件大小
  63. //progressEvent.total:被上传文件的总大小
  64. let complete = (progressEvent.loaded / progressEvent.total ).toFixed(2) * 100 ;
  65. this.percentage = complete;
  66. if (this.percentage >= 100){
  67. this.dialogVisible = true
  68. }
  69. },
  70. headers: {
  71. 'Content-Type': 'multipart/form-data'
  72. }
  73. };
  74. this.$axios.post(this.url,fd,config)
  75. .then((res)=>{
  76. })
  77. .catch((err)=>{
  78. })
  79. },
  80. handleExceed(){
  81. },
  82. submitUpload(){
  83. this.loading = true;
  84. this.tips = '正在上传中。。。';
  85. this.$refs.upload.submit();
  86. },
  87. ensure(){
  88. this.dialogVisible = false;
  89. this.loading = false;
  90. }
  91. }
  92. }
  93. </script>
  94. <style scoped>
  95. .uploadfile{
  96. width: 200px;
  97. height: 200px;
  98. position: absolute;
  99. top: 50%;
  100. left: 50%;
  101. margin-left: -100px;
  102. margin-top: -100px;
  103. }
  104. .loading{
  105. position: absolute;
  106. left: 0;
  107. top: 0;
  108. right: 0;
  109. bottom: 0;
  110. background: black;
  111. opacity: 0.8;
  112. }
  113. .progress{
  114. width: 200px;
  115. height: 200px;
  116. position: absolute;
  117. top: 50%;
  118. left: 50%;
  119. margin-left: -100px;
  120. margin-top: -100px;
  121. }
  122. .tips{
  123. color: #409eff;
  124. position: absolute;
  125. top: 50%;
  126. left: 50%;
  127. margin-left: -100px;
  128. margin-top: -150px;
  129. }
  130. </style>

5.效果演示

主要说明原理,UI就自行发挥吧。

vue+element UI + axios封装文件上传及进度条组件的更多相关文章

  1. atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7

    atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7 1. 实现原理 1 2. 大的文件上传原理::使用applet 1 3. 新的bp 2 1. 性能提升---分割小文件上传 ...

  2. atitit. 文件上传带进度条 atiUP 设计 java c# php

    atitit. 文件上传带进度条 atiUP 设计 java c# php 1. 设计要求 1 2. 原理and 架构 1 3. ui 2 4. spring mvc 2 5. springMVC.x ...

  3. Flex4/Flash多文件上传(带进度条)实例分享

    要求 必备知识 本文要求基本了解 Adobe Flex编程知识和JAVA基础知识. 开发环境 MyEclipse10/Flash Builder4.6/Flash Player11及以上 演示地址 演 ...

  4. struts2多文件上传(带进度条)demo+说明

    利用plupload插件实现多文件上传,实现图片: 在jsp写入js代码: z<%@ page language="java" contentType="text/ ...

  5. BootStrap Progressbar 实现大文件上传的进度条

    1.首先实现大文件上传,如果是几兆或者几十兆的文件就用基本的上传方式就可以了,但是如果是大文件上传的话最好是用分片上传的方式.我这里主要是使用在客户端进行分片读取到服务器段,然后保存,到了服务器段读取 ...

  6. Struts2文件上传带进度条,虽然不是很完美

    好久没有写东西,最近在做个项目,要用到文件h 传的,以前虽然也做上传,但是总觉得不好用 ,现在和队友合作做了一个带进度条的上传,觉得还行~~和大家分享一下. 首先说一下大概是这样实现的,在我们平时的上 ...

  7. ajax异步文件上传和进度条

    一.ajax异步文件上传 之前有说过在form表单内的文件上传,但是会刷新页面,下面就来实现不刷新页面的异步文件上传 <div class="uploding_div"> ...

  8. springMVC+ajax 文件上传 带进度条

    前端代码: <form id= "uploadForm"> <p >指定文件名: <input type="text" name= ...

  9. html5拖拽事件 xhr2 实现文件上传 含进度条

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

随机推荐

  1. STM32之串口DMA接收不定长数据

    STM32之串口DMA接收不定长数据 引言 在使用stm32或者其他单片机的时候,会经常使用到串口通讯,那么如何有效地接收数据呢?假如这段数据是不定长的有如何高效接收呢? 同学A:数据来了就会进入串口 ...

  2. oracle查询当前用户下所有的表,包括所有的字段

    oracle查询当前用户下所有的表,包括所有的字段 背景: ​ 前两天接到一个需求,做一个展示所有表名,表备注,表数据,表字段数,点击查看按钮查看字段名和注释,支持导出. 在Oracle中,可用使用视 ...

  3. Redis Sentinel(哨兵核心机制) 初步深入

    ##### 1.Redis 的 Sentinel 系统用于管理多个 Redis 服务 该系统执行以下三个任务:  1.监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务 ...

  4. vue图片点击放大预览

    第一种:viewerjs使用介绍(PC.移动端都兼容) 1.先安装依赖 npm install v-viewer --save 2.main.js内引用并注册调用 //main.js import V ...

  5. Java反序列化漏洞总结

    本文首发自https://www.secpulse.com/archives/95012.html,转载请注明出处. 前言 什么是序列化和反序列化 Java 提供了一种对象序列化的机制,该机制中,一个 ...

  6. Python开发【第十一篇】函数

    函数 什么是函数? 函数是可以重复执行的语句块,可以重复调用并执行函数的面向过程编程的最小单位. 函数的作用: 函数用于封装语句块,提高代码的重用性,定义用户级别的函数.提高代码的可读性和易维护性. ...

  7. 部署在本服务器上,Jenkins无法启动Tomcat

    今天在部署Jenkins的过程中,有一个需要部署在本服务器上的Web服务,而且Jenkins给我的反馈也是成功的部署了,且Tomcat启动起来了,但是实际打开项目发现其实并没有启动起来 在查看日志的时 ...

  8. opencv::自定义角点检测

    #include <opencv2/opencv.hpp> #include <iostream> #include <math.h> using namespac ...

  9. vue——前端跨域

    ***针对的是不同域名.不同协议的跨域: 1.找到config文件中开发环境的配置文件——dev.env.js,在里面将要跨域的域名配置进去 2.找到config文件中线上环境的配置文件——prod. ...

  10. pycharm中拉取新分支代码

    将本地代码由主分支切换到新分支 切换成功