ajax 下载显示进度

本文介绍了一个简单的自动化下载工具的实现过程,并通过Chrome浏览器进行了测试验证。工具利用了XMLHttpRequest对象进行文件下载,并实时更新下载进度。通过事件监听实现了下载状态的动态跟踪,确保下载过程的顺利进行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!doctype html>

<html class="no-js">

    <head>

        <meta charset="utf-8">

        <title>ajax</title>

        <meta http-equiv="X-UA-Compatible" content="IE=edge">

        <meta name="description" content="">

        <meta name="keywords" content="">

        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

        <!-- Set render engine for 360 browser -->

        <meta name="renderer" content="webkit">

        <!-- No Baidu Siteapp-->

        <meta http-equiv="Cache-Control" content="no-siteapp"/>

        <!--<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.5&ak=mVXVL4BToC5F7LZYE5ARS5ON"></script>-->

    </head>

    <body>

        <div>

            进度:<span id='progress'>0%</span><br/>

            <button onclick="download()">开始下载</button>

        </div>

        <script type="text/javascript">

         var http=new XMLHttpRequest(),//新建一个HTTP请求对象

         progress=document.getElementById('progress');//显示进度

         function download(){

         http.open('GET','./dowload.zip',true);

         http.onreadystatechange=function(){

         if(http.readyState===4){

         progress.innerHTML='ok';

         };

         }

//监听下载进度变化事件

         http.addEventListener('progress',function(evt){

         if(evt.lengthComputable){

         var percent=parseInt((evt.loaded/evt.total)*100);//计算百分比

         progress.innerHTML=percent+'%';

         }

         },false);

         http.send();

     }

        </script>

    </body>

</html>

chrome测试通过


转载于:https://my.oschina.net/u/2320308/blog/487778

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值