http网络请求与下载进度

2024-09-08 03:04
文章标签 http 请求 网络 下载 进度

本文主要是介绍http网络请求与下载进度,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Http_request 目录

一、XMLHttpRequest

在使用 Fetch API 进行网络请求时,原生的 Fetch API 并不直接支持获取下载进度的功能,因为 Fetch API 主要是基于 Promise 的,它主要关注于请求的成功或失败,以及响应数据的处理,而不直接处理像进度跟踪这样的底层细节。

不过,你可以通过一些技巧或方法间接实现下载进度的跟踪。以下是一些常用的方法:

1. 使用 XMLHttpRequest

虽然 Fetch API 较为现代,但如果你需要跟踪下载进度,XMLHttpRequest 可能是一个更好的选择。XMLHttpRequest 提供了 onprogress 事件,可以用来追踪下载进度。

var xhr = new XMLHttpRequest();
xhr.open('GET', 'your-file-url', true);
xhr.responseType = 'blob';xhr.onprogress = function (e) {if (e.lengthComputable) {var percentComplete = (e.loaded / e.total) * 100;console.log(percentComplete + '% downloaded');}
};xhr.onload = function () {if (this.status == 200) {// 处理响应数据}
};xhr.send();

在上述代码中,我们创建了一个 XMLHttpRequest 对象,并设置了 onprogress 事件处理函数。在该函数中,通过判断 e.lengthComputable 是否为真,来确定是否可以计算下载进度。如果可以计算,则通过 e.loaded 和 e.total 计算出已下载的百分比,并将其打印到控制台

2.XMLHttpRequest 的进一步封装

   xhrToDownload(options, onProgress, onSuccess, onError) {return new Promise((resolve, reject) => {const xhr = new XMLHttpRequest();xhr.open(options.method || 'GET', options.url);xhr.responseType = options.responseType || 'blob';xhr.onprogress = function(e) {if (e.lengthComputable) {const progress = (e.loaded / e.total) * 100;onProgress && onProgress(progress);}};xhr.onload = function(e) {if (xhr.status === 200) {// onSuccess && onSuccess(xhr.response);console.log('上传成功', xhr);resolve({ status: xhr.status, data: xhr.response })} else {onError && onError(xhr.statusText);reject({ status: xhr.status, data: xhr.statusText }); // 拒绝 Promise}}xhr.onerror = function(e) {onError && onError(xhr.statusText);reject({ status: xhr.status, data: xhr.statusText }); // 拒绝 Promise};xhr.send();});},

这个示例进一步封装了 XMLHttpRequest,使其可以返回一个 Promise,方便进行异步处理。

3. 创建 a 标签下载

        downloadFile(blob, fileName = '2.mp4') {// 创建a 标签const a = document.createElement('a');const blobUrl = URL.createObjectURL(blob);a.setAttribute('href', blobUrl);a.setAttribute('download', fileName);a.style.display = 'none';document.body.appendChild(a);a.click();document.body.removeChild(a);URL.revokeObjectURL(blobUrl)},

使用实例:

    async downloadVideo(row) {const url = row.path;if (!url) {return;}var index = this.tableData.findIndex(item => item.id === row.id);// 使用示例const response = await this.xhrToDownload({ url: url }, (progress) => {console.log('Download progress:', progress);if (index !== -1) {this.tableData[index].downLoadProgress = progress}}, (res) => {// 这里处理 Blob,例如保存到 IndexedDB、FileSystem API 或其他console.log('Download successful:', res);// 如果你确实需要下载文件,可以在这里创建 <a> 标签并触发点击}, (error) => {console.error('Download failed:', error);})if (response && response.status === 200) {this.downloadFile(response.data)}},

二、 Fetch API

1. 使用 Fetch API

  • 链式调用

            fetch(url).then(res => {return res.blob()}).then(res => {console.log('res', res);})
    
  • async - await 语法糖

        // const response = await fetch(url)// const blod = await response.blob()

2. 使用 Stream API 和 ReadableStream

Fetch API 支持响应体作为 ReadableStream,但直接使用它来跟踪进度可能不太直观。不过,你可以通过监听流的读取操作来大致估计进度(虽然这通常不如 XMLHttpRequest 那样精确)。

    //your_file_url fetch('http://127.0.0.1:456/proxy/DJI_0003.MP4')fetch('http://127.0.0.1:456/proxy/DJI_0003.MP4').then(response=>{console.log(response);const reader = response.body.getReader() //  ReadableStreamconst contentLength = response.headers.get('content-length')let readTotal = 0if(!contentLength){console.log('无法获取进度');return}const sizeTotal = parseInt(contentLength)const chunks =[]function read(){reader.read().then(({done,value})=>{if(done){console.log('下载完成');const type = response.headers.get('content-type')const blob = new Blob(chunks,{type})return}readTotal += value.lengthconst progress = Math.ceil(readTotal/sizeTotal*100)console.log('下载进度:',progress);chunks.push(value)read()})}read()})

注意:上面的代码示例并不直接计算下载进度,因为 ReadableStream API 并不直接提供总大小信息(除非你在响应头中通过 Content-Length 获取)。你需要有一个方式来获取文件的总大小,以便能够计算进度。

3. 使用fetch下载并获取进度

简单获取下载进度fetchToDownlod(url, options, onProgress, onSuccess, onError) {try {// eslint-disable-next-line no-async-promise-executorreturn new Promise(async(resolve, reject) => {const response = await fetch(url, options);const reader = response.body.getReader();// Step 2:获得总长度(length)const contentLength = +response.headers.get('Content-Length');console.log('contentLength', contentLength);// Step 3:读取数据let receivedLength = 0; // 当前接收到了这么多字节const chunks = []; // 接收到的二进制块的数组(包括 body)// eslint-disable-next-line no-constant-conditionwhile (true) {const { done, value } = await reader.read();if (done) {// 如果没有更多的数据可读,则退出循环break;}chunks.push(value);receivedLength += value.length;const progress = Math.round(receivedLength / contentLength * 100);onProgress && onProgress(progress);}// 将响应体转换为 Blob 对象const blob = new Blob(chunks, { type: 'application/octet-stream' });if (response.status === 200) {resolve({ status: response.status, blob });}if (response.status === 404) {reject({ status: response.status, blob });}});} catch (err) {console.log('err', err);return Promise.reject(err);}},

调用实例:

       async downloadVideo(row) {const url = row.path;if (!url) {return;}let fileName = 'text.mp4'const lastIndex = url.lastIndexOf('/');if (lastIndex !== -1) {fileName = url.substring(lastIndex + 1);}var index = this.tableData.findIndex(item => item.id === row.id);const options = {method: 'GET', // *GET, POST, PUT, DELETE, etc.mode: 'cors', // no-cors, *cors, same-origincache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cachedcredentials: 'same-origin', // include, *same-origin, omitresponseType: 'blob', //重要代码'Access-Control-Allow-Origin': '*','Access-Control-Allow-Credentials': true,headers: {'Content-Type': 'application/json'}}const res = await this.fetchToDownlod(url, options, (progress) => {// console.log('Download progress:', progress);if (index !== -1) {this.tableData[index].downLoadProgress = progress}})console.log('res', res);if (!res || res.status !== 200) {return this.$message.error('下载失败')}this.downloadFile(res.blob, fileName)},

结论

如果你的应用需要精确地跟踪下载进度,并且你的环境允许,使用 XMLHttpRequest 可能是最直接和简单的方法。如果你正在寻找更现代的方法,并可以接受一些复杂性,你可以考虑使用 Service Workers 或 Stream API 来实现类似的功能。

这篇关于http网络请求与下载进度的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1146928

相关文章

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty

Debian 13升级后网络转发等功能异常怎么办? 并非错误而是管理机制变更

《Debian13升级后网络转发等功能异常怎么办?并非错误而是管理机制变更》很多朋友反馈,更新到Debian13后网络转发等功能异常,这并非BUG而是Debian13Trixie调整... 日前 Debian 13 Trixie 发布后已经有众多网友升级到新版本,只不过升级后发现某些功能存在异常,例如网络转

SpringBoot请求参数传递与接收示例详解

《SpringBoot请求参数传递与接收示例详解》本文给大家介绍SpringBoot请求参数传递与接收示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录I. 基础参数传递i.查询参数(Query Parameters)ii.路径参数(Path Va

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

使用Python的requests库来发送HTTP请求的操作指南

《使用Python的requests库来发送HTTP请求的操作指南》使用Python的requests库发送HTTP请求是非常简单和直观的,requests库提供了丰富的API,可以发送各种类型的HT... 目录前言1. 安装 requests 库2. 发送 GET 请求3. 发送 POST 请求4. 发送

Python多线程实现大文件快速下载的代码实现

《Python多线程实现大文件快速下载的代码实现》在互联网时代,文件下载是日常操作之一,尤其是大文件,然而,网络条件不稳定或带宽有限时,下载速度会变得很慢,本文将介绍如何使用Python实现多线程下载... 目录引言一、多线程下载原理二、python实现多线程下载代码说明:三、实战案例四、注意事项五、总结引

Python开发简易网络服务器的示例详解(新手入门)

《Python开发简易网络服务器的示例详解(新手入门)》网络服务器是互联网基础设施的核心组件,它本质上是一个持续运行的程序,负责监听特定端口,本文将使用Python开发一个简单的网络服务器,感兴趣的小... 目录网络服务器基础概念python内置服务器模块1. HTTP服务器模块2. Socket服务器模块

Go语言使用net/http构建一个RESTful API的示例代码

《Go语言使用net/http构建一个RESTfulAPI的示例代码》Go的标准库net/http提供了构建Web服务所需的强大功能,虽然众多第三方框架(如Gin、Echo)已经封装了很多功能,但... 目录引言一、什么是 RESTful API?二、实战目标:用户信息管理 API三、代码实现1. 用户数据

Python WSGI HTTP服务器Gunicorn使用详解

《PythonWSGIHTTP服务器Gunicorn使用详解》Gunicorn是Python的WSGI服务器,用于部署Flask/Django应用,性能高且稳定,支持多Worker类型与配置,可处... 目录一、什么是 Gunicorn?二、为什么需要Gunicorn?三、安装Gunicorn四、基本使用启