前端开发攻略---使用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>
相关推荐
马立杰1 小时前
H3CNE-33-BGP
运维·网络·h3cne
Mason Lin1 小时前
2025年1月22日(网络编程 udp)
网络·python·udp
还是鼠鼠1 小时前
图书管理系统 Axios 源码 __删除图书功能
前端·javascript·vscode·ajax·前端框架·node.js·bootstrap
字节全栈_rJF2 小时前
概述、 BGP AS 、BGP 邻居、 BGP 更新源 、BGP TTL 、BGP路由表、 BGP 同步
网络·智能路由器·php
EchoToMe2 小时前
电信传输基本理论/5G网络层次架构——超三万字详解:适用期末考试/考研/工作
网络·5g·架构
轻口味2 小时前
Vue.js `Suspense` 和异步组件加载
前端·javascript·vue.js
doubt。2 小时前
8.攻防世界Web_php_wrong_nginx_config
网络·安全·web安全·网络安全
好记性+烂笔头3 小时前
4 Spark Streaming
大数据·ajax·spark
没有名字的小羊3 小时前
Cyber Security 101-Build Your Cyber Security Career-Security Principles(安全原则)
运维·网络·安全
m0_zj3 小时前
8.[前端开发-CSS]Day08-图形-字体-字体图标-元素定位
前端·css