编写了一个vue组件,可以实时显示文件上传和下载时候的进度

<template>
    <div v-show="circleProgress_wrapper_panel_statue"  class="circleProgress_wrapper_panel">
        <div class="circleProgress_wrapper">
            <div class="wrapper right">
                <div v-bind:style="{transform:'rotate('+leftDeg + 'deg)'}"  class="circleProgress rightcircle"></div>
            </div>
            <div class="wrapper left">
                <div v-bind:style="{transform:'rotate('+rightDeg + 'deg)'}"  class="circleProgress leftcircle"></div>
            </div>
         </div>
         <div class="backRect">
             <img src="../img/back.png" />
         </div>
     </div>
</template>
<script>
export default {
    data:function(){
        return {
            circleProgress_wrapper_panel_statue:false,
            leftDeg:0,
            rightDeg:0
        }
    },
    methods:{
        //上传文件
    loadFile:function (url, fileList,parm, suc) {
                let len = fileList.length,
                        _this = this;
                if(len == 0){
                    return;
                }
        this.showCircleProgressWrapperPanel();
        let formData = new FormData();
                for(let i = 0; i < len; i++){
                    formData.append("files"+i, fileList[i]);
                }
                for(let key in parm){
                    formData.append(key, parm[key]);
                }
        let xhr = new XMLHttpRequest();
        xhr.timeout = 15000;
        //xhr.responseType = "json";
        xhr.open('POST', url, true);
        xhr.onload = function (e) {
            if (this.status == 200 || this.status == 304) {
                suc(this.responseText);
            }
        };
        xhr.ontimeout = function (e) { alert("超时") };
        xhr.onerror = function (e) { alert("error:"+e) };
        xhr.upload.onprogress = function (e) {
            _this.updateCircleProgressWrapper(e.loaded, e.total);
        };
        xhr.send(formData);
    },
        //显示文件
    show:function (url, suc) {
                let _this = this;
        this.showCircleProgressWrapperPanel();
        let xhr = new XMLHttpRequest();
        xhr.open('GET', url);
        xhr.responseType = "blob";
        xhr.onprogress = function (e) {
            _this.updateCircleProgressWrapper(e.loaded, e.total);
        };
        xhr.onload = function () {
            if (this.status === 200) {
                                let blob = this.response;
                                suc(blob);
            }
        }
        xhr.send();
    },
        test:function(){
            this.showCircleProgressWrapperPanel();
            let _this = this,
                    i = 0,
                    total = 10;
            window.interval = window.setInterval(function () {
                _this.updateCircleProgressWrapper(i, total);
                i = i + 1;
                if(i > total){
                    window.clearInterval(window.interval);
                }
            }, 100);
        },
    //根据上传字节数计算角度
    updateCircleProgressWrapper:function (loaded, total) {
        let deg = 360 * loaded / total;
        this.updateCircleProgressRotate(deg);
    },
    //隐藏滚动条
    hideCircleProgressWrapperPanel:function () {
                this.circleProgress_wrapper_panel_statue = false;
        this.setCircleProgressRotate(45, 45);
    },
    //显示滚动条
    showCircleProgressWrapperPanel:function () {
        this.circleProgress_wrapper_panel_statue = true;
    },
    //更新滚动条角度
    updateCircleProgressRotate:function (deg) {
        let defaultDeg = deg;
        if (deg <= 180) {
            deg = deg + 45;
            this.setCircleProgressRotate(deg, 45);
        } else {
            (deg > 360) ? deg = 360 : "";
            deg = deg - 180;
            deg = deg + 45;
            this.setCircleProgressRotate(225, deg);
        }
        if (defaultDeg == 360) {
            this.hideCircleProgressWrapperPanel();
        }
    },
    //设置滚动条角度
    setCircleProgressRotate:function(leftDeg, rightDeg) {
                this.leftDeg = leftDeg;
                this.rightDeg = rightDeg;
    }
    },
    mounted:function(){

    }
}
</script>
<style scoped>
    .circleProgress_wrapper_panel
    {
            position:fixed;
            top:0;
            left:0;
            right:0;
            bottom:0;
            background-color:rgba(0,0,0,0.8);
    }
    .circleProgress_wrapper
    {
            position:absolute;
            top:50%;
            left:50%;
            width: 100px;
            height: 100px;
            -webkit-transform: translate(-50%,-50%);
            transform: translate(-50%,-50%);
    }
    .wrapper{
            width: 50px;
            height: 100px;
            position: absolute;
            top:0;
            overflow: hidden;
    }
    .right{
            right:0;
    }
    .left{
            left:0;
    }
    .circleProgress{
            width: 80px;
            height: 80px;
            border:10px solid transparent;
            border-radius: 50%;
            position: absolute;
            top:0;
            -webkit-transform: rotate(45deg);
            transform: rotate(45deg);
    }
    .rightcircle{
            border-top:10px solid #767676;
            border-right:10px solid #767676;
            right:0;
            -webkit-transform: rotate(45deg);
            transform: rotate(45deg);
    }
    .leftcircle{
            border-bottom:10px solid #767676;
            border-left:10px solid #767676;
            left:0;
            -webkit-transform: rotate(45deg);
            transform: rotate(45deg);
    }
    .backRect
    {
         position:absolute;
         top:50%;
         left:50%;
         width:79px;
         height:79px;
         border:10px solid #e5e5e5;
         background-color:White;
         border-radius: 50%;
         -webkit-transform: translate(-50%,-50%);
         transform: translate(-50%,-50%);
         z-index:-1;
    }
    .backRect img
    {
        width:100%;
        position:absolute;
        top:50%;
        left:50%;
         -webkit-transform: translate(-50%,-50%);
         transform: translate(-50%,-50%);
    }
</style>

文件上传使用方法:

引入组件:import fileLoading from '../component/fileLoading.vue'

注册组件:

components: {
fileLoading:fileLoading
}

声明组件:<fileLoading ref="fileLoadingPonent"></fileLoading>

使用:

let fileData = [],
                        _this = this,
                        imgList = this.imgList,
                        len = this.imgList.length,
                        item = null;
                for(let i = 0; i < len; i++){
                    item = imgList[i];
                    fileData.push(item.data);//将多个文件对应的file对象放入数组
                }
                this.$refs.fileLoadingPonent.loadFile(Config.report,fileData,{content:this.reportContent},function(data){
                    let result = JSON.parse(data);
                    if(result.res == "1"){
                        _this.$store.commit('report/setReportContent',{value:""})
                        _this.$store.commit('report/clearImgList',{})
                        _this.clearIcon();
                    }else{
                        alert(result.msg);
                    }
                });

下载文件使用:

  使用场景:图片很大的时候,先加载缩略图,然后点击缩略图再加载大图

  引入组件:import fileLoading from '../component/fileLoading.vue'

  注册组件:

  components: {
    fileLoading:fileLoading
  }

  声明组件:<fileLoading ref="fileLoadingPonent"></fileLoading>

  使用:

  

loadBigImg:function(src,index,event){
      let _this = this;
      let NSrc = src.replace("_s","");//缩略图和大图的区别是缩略图在大图的名字后面多了_s,如:大图:name.jpg,对应的缩略图:name_s.jpg
      let NPath = Config.hostImg + NSrc;//大图的显示地址
      let target = event.currentTarget;
      this.$refs.fileLoadingPonent.show(NPath,function(blob){
        let imgEle = document.createElement("img");//创建新图片控件
        let parentNode = target.parentNode;
        parentNode.removeChild(parentNode.getElementsByTagName("img")[0]);//删除之前的缩略图
        imgEle.style.width = "100%";
        imgEle.onload = function (e) {
            window.URL.revokeObjectURL(imgEle.src); //释放。
        };
        imgEle.src = window.URL.createObjectURL(blob);  //有问题,将blob加载到img中 由于blob太大 会有性能影响 应该怎么在加载之后 如何释放呢:
        parentNode.appendChild(imgEle);
        //_this.$store.commit('details/changeImgList',{value:NSrc,index:index});
        target.style.display = "none";//隐藏提示点击加载文字
      });
    }

文件上传下载显示进度(vue)的更多相关文章

  1. Retrofit2文件上传下载及其进度显示

    序 前面一篇文章介绍了Retrofit2的基本使用,这篇文章接着介绍使用Retrofit2实现文件上传和文件下载,以及上传下载过程中如何实现进度的显示. 文件上传 定义接口 1 2 3 @Multip ...

  2. Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现)(转)

    Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现) 相信通过Asp.Net的服务器控件上传文件在简单不过了,通过AjaxToolkit控件实现上传进度也不是什么难事,为什么还要自己辛辛苦 ...

  3. 【原创】用JAVA实现大文件上传及显示进度信息

    用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/grayprince/UploadBigFil ...

  4. 用JAVA实现大文件上传及显示进度信息

    一. 大文件上传基础描述: 各种WEB框架中,对于浏览器上传文件的请求,都有自己的处理对象负责对Http MultiPart协议内容进行解析,并供开发人员调用请求的表单内容. 比如: Spring 框 ...

  5. Asp.Net 无刷新文件上传并显示进度条的实现方法及思路

    相信通过Asp.Net的服务器控件上传文件在简单不过了,通过AjaxToolkit控件实现上传进度也不是什么难事,为什么还要自己辛辛苦苦来 实现呢?我并不否认”拿来主义“,只是我个人更喜欢凡是求个所以 ...

  6. 仵航说 前后端分离,文件上传下载(springBoot+vue+elementUI)仵老大

    1.介绍 ​ 本文主要是介绍前后端分离的上传下载,后端使用的是SpringBoot,持久层用的是mybatis-plus,前端用的Vue,UI用的elementUI,测试了一下,文本,图片,excel ...

  7. 使用Typescript重构axios(二十五)——文件上传下载进度监控

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...

  8. SpringMVC文件上传下载

    在Spring MVC的基础框架搭建起来后,我们测试了spring mvc中的返回值类型,如果你还没有搭建好springmvc的架构请参考博文->http://www.cnblogs.com/q ...

  9. JavaWeb实现文件上传下载功能实例解析

    转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...

随机推荐

  1. H/s:哈希率单位转换

    哈系率说明 挖矿能力是通过寻找矿工可以执行的地块的尝试次数来衡量的.每次尝试都包括创建一个唯一的块候选项,并通过SHA-256d(一种加密哈希函数)创建块候选项的摘要.或者,简而言之,哈希.由于这是一 ...

  2. kickstart ---无人值守安装

    kickstart 实战 http://man.linuxde.net/download/CentOS_6_9镜像 安装: 关闭防火墙 且在编辑虚拟网络的vnet8中要取消使用本地dhcp项 1.yu ...

  3. MACD技术的高级应用--MACD与波浪

    在开始分析MACD指标之前,我想我们必须先从思想上认同以下两点,否则本文的研究就没有意义.  1)趋势在一段时间内是可以把握的:  2)每个指标都有有效的时候,没有指标会始终有效.我们就是要搞清楚指标 ...

  4. java之Spring集成CXF简单调用

    简介 Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.CXF 继承了 Celtix 和 X ...

  5. python框架之Django(3)-模版

    常用语法 符号 {{...}} # 变量相关 {%...%} # 逻辑相关 {#...#} # 注释 使用变量 def test(request): name = '张三' age = 19 retu ...

  6. select,poll,epoll

    1. Epoll 是何方神圣? Epoll 可是当前在 Linux 下开发大规模并发网络程序的热门人选, Epoll 在 Linux2.6 内核中正式引入,和 select 相似,其实都 I/O 多路 ...

  7. 131A

    #include <stdio.h> #include <string.h> #include <stdbool.h> #define MAXSIZE 105 in ...

  8. 对stm32寄存器的理解(个人理解,大神轻喷)

    学习了stm32有一年了,今天想来写写自己对寄存器的理解,帮助那些有志学习stm32的朋友们少走一些弯路. ---------------------------------------------- ...

  9. [ Build Tools ] Repositories

    仓库介绍 http://hao.jobbole.com/central-repository/ https://my.oschina.net/pingjiangyetan/blog/423380 ht ...

  10. CSS几个要点补充

    css的font-size属性一定要带px单位,font-weight:700[千万不要带单位哦]就等于font-weight:bold加粗 1.如何像淘宝页面一样,随着浏览器的减小而保持内容区大小不 ...