当前位置: 首页 > news >正文

jq/h5 实现实时获取大文件下载进度

废话不多说,直接上代码

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>下载进度条</title>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<style type="text/css">
    .containerBar {
        width: 521px;
        border: 1px solid #008fcd;
        height: 21px;
        border-radius: 10px;
        overflow: hidden;
        margin: auto;
    }

    #bar {
        background: #008fcd;
        float: left;
        height: 100%;
        text-align: center;
        line-height: 150%;
    }

    #total {
        margin-bottom: 20px;
    }

    /* 加载中 */
    #loading {
        display: none;
        height: 32px;
    }

    #loading span {
        position: absolute;
        left: 40px;
    }

    .demo1 {
        width: 4px;
        height: 4px;
        border-radius: 2px;
        background: #008fcd;
        float: left;
        margin: 5px 3px;
        animation: demo1 linear 1s infinite;
        -webkit-animation: demo1 linear 1s infinite;
    }

    .demo1:nth-child(1) {
        animation-delay: 0s;
    }

    .demo1:nth-child(2) {
        animation-delay: 0.15s;
    }

    .demo1:nth-child(3) {
        animation-delay: 0.3s;
    }

    .demo1:nth-child(4) {
        animation-delay: 0.45s;
    }

    .demo1:nth-child(5) {
        animation-delay: 0.6s;
    }

    @keyframes demo1 {

        0%,
        60%,
        100% {
            transform: scale(1);
        }

        30% {
            transform: scale(2.5);
        }
    }

    @-webkit-keyframes demo1 {

        0%,
        60%,
        100% {
            transform: scale(1);
        }

        30% {
            transform: scale(2.5);
        }
    }
</style>

<body>
    <div style="text-align: center;">

        <button onclick="onClick()" style="width: 200px;height:200px;margin: 15px;">点击下载</button>

        <div class="containerBar">
            <div id="bar" style="width:0%;"></div>
        </div>
        <span id="total">0%</span>
        <div id="loading">
            <!-- <span>正在生成码包</span> -->
            <div class="demo1"></div>
            <div class="demo1"></div>
            <div class="demo1"></div>
            <div class="demo1"></div>
            <div class="demo1"></div>
        </div>
    </div>
</body>
<script type="text/javascript">
    function onClick() {

        var page_url = 'http://xxxxxxx.cn/3_21%20(4).apk';
        // var req = new XMLHttpRequest();

        var req;
        if (window.XMLHttpRequest) {
            //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
            req = new XMLHttpRequest();
        }else {
            // IE6, IE5 浏览器执行代码
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }

        req.open("get", page_url, true);
        $("#total").css('display', 'none');
        req.addEventListener("progress", function (evt) {
            console.log(evt)
            if (evt.lengthComputable) {
                $("#total").css('display', 'block');
                $("#loading").css('display', 'inline-block')
                var percentComplete = Number((evt.loaded / evt.total).toString().match(/^\d+(?:\.\d{0,2})?/));
                console.log(percentComplete);
                let bar = document.getElementById("bar")
                console.log(bar)
                if (bar != null && bar != 'bull') {
                    console.log(bar)
                    bar.style.width = (percentComplete * 100) + "%";
                    $("#total").html(Math.floor(percentComplete * 100) + "%");
                }
                if (percentComplete == 1) {
                    setTimeout(function () {
                        //下载完成要执行的操作
                        $("#total").html('下载完成')
                        $("#loading").css('display', 'none')
                    }, 500);
                }
            }
        }, false);
        req.responseType = "blob";
        req.onreadystatechange = function () {
            if (req.readyState === 4 && req.status === 200) {
                var filename = 'test.apk';
                if (typeof window.chrome !== 'undefined') {
                    // Chrome version
                    var link = document.createElement('a');
                    link.href = window.URL.createObjectURL(req.response);
                    link.download = filename;
                    link.click();
                } else if (typeof window.navigator.msSaveBlob !== 'undefined') {
                    // IE version
                    var blob = new Blob([req.response], { type: 'application/vnd.android.package-archive' });
                    window.navigator.msSaveBlob(blob, filename);
                } else {
                    // Firefox version
                    var file = new File([req.response], filename, { type: 'application/vnd.android.package-archive' });
                    window.open(URL.createObjectURL(file));
                }
            }
        };
        req.send();
    }

</script>

</html>

效果

在这里插入图片描述
vue的可以看我另一篇文章:vue 实现实时获取大文件下载进度

相关文章:

  • 【从零开始的Java开发】2-9-3 油画商城静态网页案例
  • 计算机毕业设计ssm+vue基本微信小程序的个人健康管理系统
  • 【PTHREAD】线程互斥与同步之互斥锁
  • vscode自动生成testbench
  • 【流放之路-装备制作篇】
  • BLE错误码全面解析连接失败原因错误码解析BLE Disconnect Reason
  • Tensorflow pb模型转tflite,并量化
  • 【PTHREAD】线程状态
  • 网易云音乐项目————项目准备
  • 计算机网络——应用层の选择题整理
  • LabVIEW通过网络传输数据
  • 【PTHREAD】线程属性
  • 如何做好项目管理?项目管理和团队协作是关键
  • 《嵌入式 – GD32开发实战指南》第20章 GD32的存储结构
  • Vue模块语法上(插值指令过滤器计算属性-监听属性)
  • Google 是如何开发 Web 框架的
  • C# 免费离线人脸识别 2.0 Demo
  • CSS 提示工具(Tooltip)
  • ES6 学习笔记(一)let,const和解构赋值
  • idea + plantuml 画流程图
  • JavaScript DOM 10 - 滚动
  • Java新版本的开发已正式进入轨道,版本号18.3
  • Laravel深入学习6 - 应用体系结构:解耦事件处理器
  • Netty源码解析1-Buffer
  • nodejs调试方法
  • Python3爬取英雄联盟英雄皮肤大图
  • Spark VS Hadoop:两大大数据分析系统深度解读
  • Traffic-Sign Detection and Classification in the Wild 论文笔记
  • 代理模式
  • 浅谈Golang中select的用法
  • 使用 Docker 部署 Spring Boot项目
  • 自动记录MySQL慢查询快照脚本
  • elasticsearch-head插件安装
  • ionic入门之数据绑定显示-1
  • 阿里云ACE认证之理解CDN技术
  • ​2021半年盘点,不想你错过的重磅新书
  • #QT项目实战(天气预报)
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • (0)Nginx 功能特性
  • (02)Cartographer源码无死角解析-(03) 新数据运行与地图保存、加载地图启动仅定位模式
  • (C语言)共用体union的用法举例
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (ZT) 理解系统底层的概念是多么重要(by趋势科技邹飞)
  • (附源码)spring boot网络空间安全实验教学示范中心网站 毕业设计 111454
  • (附源码)ssm户外用品商城 毕业设计 112346
  • (离散数学)逻辑连接词
  • (排序详解之 堆排序)
  • (十三)Maven插件解析运行机制
  • (五)网络优化与超参数选择--九五小庞
  • (转)拼包函数及网络封包的异常处理(含代码)
  • .NET CLR Hosting 简介
  • .Net Winform开发笔记(一)
  • .net 前台table如何加一列下拉框_如何用Word编辑参考文献
  • .net遍历html中全部的中文,ASP.NET中遍历页面的所有button控件
  • .net最好用的JSON类Newtonsoft.Json获取多级数据SelectToken