前端开发攻略---使用AJAX监控网络请求进度

1、XHR实现

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <progress value="0" max="0"></progress>
    <script>
      const progress = document.querySelector('progress')
      function readFile2() {
        const xhr = new XMLHttpRequest()
        xhr.open('GET', 'http://127.0.0.1:8888/ttt.txt')

        xhr.addEventListener('progress', e => {
          // e.loaded是当前加载大小 e.total是总大小
          console.log(e.loaded, e.total)
          progress.setAttribute('value', e.loaded)
          progress.setAttribute('max', e.total)
        })

        // xhr.onload = function () {
        //   if (xhr.status === 200) {
        //     console.log('响应内容:', xhr.responseText)
        //   } else {
        //     console.error('请求失败,状态码:', xhr.status)
        //   }
        // }
        xhr.send() // 发送请求
      }
      readFile2()
    </script>
  </body>
</html>

2、Fetch实现

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <progress value="0" max="0"></progress>
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
    <script>
      const progress = document.querySelector('progress')
      async function readFile() {
        const url = 'http://127.0.0.1:8888/ttt.txt'
        const resp = await fetch(url)
        // 从响应头中获取数据总大小
        const total = resp.headers.get('content-length')
        // 更新进度条
        progress.setAttribute('max', total)
        // 从响应体中获取一个读取器(reader),允许你逐块读取响应的数据流。
        const reader = resp.body.getReader()
        // 创建一个 TextDecoder 实例,用于将读取到的字节数据转换为文本字符串。
        const decoder = new TextDecoder()
        // 用来存储当前加载的大小
        let loaded = 0
        while (1) {
          // 使用 reader.read() 异步读取下一块数据。value 是读取到的字节,done 是一个布尔值,指示是否已读取到流的末尾。
          const { value, done } = await reader.read()
          if (done) break
          loaded += value.length
          // 更新进度条
          progress.setAttribute('value', loaded)
          // 将读取到的字节(value)解码为字符串格式。
          // const text = decoder.decode(value)
        }
      }
      readFile()
    </script>
  </body>
</html>
相关推荐
Rverdoser18 分钟前
代理服务器运行速度慢是什么原因
开发语言·前端·php
陌尘(MoCheeen)20 分钟前
技术书籍推荐(002)
java·javascript·c++·python·go
航Hang*36 分钟前
前端项目2-01:个人简介页面
前端·经验分享·html·css3·html5·webstorm
油丶酸萝卜别吃1 小时前
openlayers利用已知的三个经纬度的坐标点 , 绘制一个贝塞尔曲线
javascript
MaisieKim_1 小时前
python与nodejs哪个性能高
前端·python·node.js
Spider Cat 蜘蛛猫1 小时前
【一】浏览器的copy as fetch和copy as bash的区别
javascript·ajax·bash·逆向·fetch
Frankabcdefgh1 小时前
前端进化论·JavaScript 篇 · 数据类型
javascript·安全·面试·数据类型·操作符·初学者·原理解析
achene_ql1 小时前
WebRTC:去中心化网络P2P框架解析
网络·去中心化·webrtc·p2p
s_little_monster1 小时前
【Linux】socket网络编程之TCP
linux·运维·网络·笔记·学习·tcp/ip·学习方法
水煮白菜王1 小时前
深入理解 Webpack 核心机制与编译流程
前端·webpack·node.js