StreamSaver实现大文件下载解决方案

news2025/5/22 20:14:58

StreamSaver实现大文件下载解决方案

web端

  1. 安装 StreamSaver.js
npm install streamsaver
# 或
yarn add streamsaver
  1. 在 Vue 组件中导入
import streamSaver from "streamsaver"; // 确保导入名称正确
  1. 完整代码修正
<!--
  * @projectName: 
  * @desc: 
  * @author: duanfanchao
  * @date: 2024/06/20 10:00:00
-->
<template>
	<div class="async-table">
		<button @click="downloadLargeFile">下载大文件(带进度)</button>
		<div v-if="progress > 0">下载进度: {{ progress }}%</div>
	</div>
</template>

<script>
import streamSaver from "streamsaver";

export default {
	name: "AsyncTable",
	components: {},
	data() {
		return {
			progress: 0,
		};
	},
	methods: {
		async downloadLargeFile() {
			try {
				const fileUrl = "../系统架构师资料.zip"; // 替换为你的大文件URL
				const fileName = "largeFile.zip"; // 下载后的文件名

				// 使用 fetch 获取文件流
				const response = await fetch(fileUrl);
				if (!response.ok) throw new Error("下载失败");

				const contentLength = +response.headers.get("content-length");
				let downloadedBytes = 0;

				const fileStream = streamSaver.createWriteStream(fileName);
				const reader = response.body.getReader();
				const writer = fileStream.getWriter();

				const updateProgress = (chunk) => {
					downloadedBytes += chunk.length;
					this.progress = Math.round(
						(downloadedBytes / contentLength) * 100
					);
                    console.log('updateProgress', this.progress);
				};

				const pump = async () => {
					const { done, value } = await reader.read();
					if (done) {
						await writer.close();
						return;
					}
					updateProgress(value);
					await writer.write(value);
					return pump();
				};
				await pump();

				console.log("下载完成!");
			} catch (error) {
				console.error("下载出错:", error);
			}
		},
	},
	mounted() {},
};
</script>

<style lang="less" scoped>
.async-table {
    height: 100%;
	width: 100%;
}
</style>

注意

  • StreamSaver.js 依赖 Service Worker,在 ·本地localhost 开发环境可用,但生产环境必须使用 HTTPS

node端

在 Node.js 环境下,StreamSaver.js 无法直接使用,因为它是专门为浏览器设计的库(依赖 Service Worker 和浏览器 API)。但 Node.js 本身支持流式文件处理,可以直接使用 fs 和 http/https` 模块实现大文件下载。

Node.js 实现大文件下载(替代 StreamSaver.js)

前置条件:需要安装对应的模块,如:npm i express http

推荐node版本 16.20.0

1. 使用 fs.createReadStream + res.pipe(推荐)

const express = require("express");
const fs = require("fs");
const path = require("path");

const app = express();
const PORT = 3001;

// 提供大文件下载
app.get("/download", (req, res) => {
    res.setHeader("Access-Control-Allow-Origin", "*"); // 允许所有来源
    res.setHeader("Access-Control-Allow-Methods", "GET"); // 允许 GET 请求

    const filePath = path.join(__dirname, "./系统架构师资料.zip"); // 文件路径
    const fileSize = fs.statSync(filePath).size; // 获取文件大小
    const fileName = path.basename(filePath); // 获取文件名

    // RFC 5987 编码(推荐)
    const encodedFileName = encodeURIComponent(fileName).replace(/'/g, "%27");
    // 设置响应头(支持断点续传)
    res.setHeader(
        "Content-Disposition",
        `attachment; filename*=UTF-8''${encodedFileName}`
    );
    res.setHeader("Content-Length", fileSize);
    res.setHeader("Content-Type", "application/octet-stream");

    // 创建可读流并管道传输到响应
    const fileStream = fs.createReadStream(filePath);
    fileStream.pipe(res); // 流式传输

    // 监听错误
    fileStream.on("error", (err) => {
        console.error("文件传输失败:", err);
        res.status(500).send("下载失败");
    });
});

app.listen(PORT, () => {
    console.log(`服务器运行在 http://localhost:${PORT}`);
});
<!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>
    <!-- <a href="http://localhost:3001/download" download>下载大文件</a> -->
    <input type="button" value="下载大文件" onclick="download()" />
    <script>
      function download() {
        fetch("http://localhost:3001/download")
          .then((response) => response.blob())
          .then((blob) => {
            const url = URL.createObjectURL(blob);
            const a = document.createElement("a");
            a.href = url;
            a.download = "large-file.zip";
            a.click();
          });
      }
    </script>
  </body>
</html>

2. 使用 http 模块(原生 Node.js)

如果不想用 Express,可以用原生 http 模块:

const http = require("http");
const fs = require("fs");
const path = require("path");

const server = http.createServer((req, res) => {
  if (req.url === "/download") {
    const filePath = path.join(__dirname, "large-file.zip");
    const fileSize = fs.statSync(filePath).size;
    const fileName = path.basename(filePath);

    res.writeHead(200, {
      "Content-Disposition": `attachment; filename="${fileName}"`,
      "Content-Length": fileSize,
      "Content-Type": "application/octet-stream",
    });

    const fileStream = fs.createReadStream(filePath);
    fileStream.pipe(res);

    fileStream.on("error", (err) => {
      console.error("下载失败:", err);
      res.end("下载失败");
    });
  } else {
    res.end("访问 /download 下载文件");
  }
});

server.listen(3000, () => {
  console.log("服务器运行在 http://localhost:3000");
});

3. 大文件分块下载(支持断点续传)

Node.js 可以支持 Range 请求,实现断点续传:

const express = require("express");
const fs = require("fs");
const path = require("path");

const app = express();
const PORT = 3002;

app.get("/download", (req, res) => {
    res.setHeader("Access-Control-Allow-Origin", "*"); // 允许所有来源
    res.setHeader("Access-Control-Allow-Methods", "GET"); // 允许 GET 请求
    const filePath = path.join(__dirname, "系统架构师资料.zip");
    const fileName = path.basename(filePath);

    // RFC 5987 编码
    const encodedFileName = encodeURIComponent(fileName).replace(/'/g, "%27");

    try {
        const fileSize = fs.statSync(filePath).size;

        // 解析 Range 请求头
        const range = req.headers.range;
        if (range) {
            const [start, end] = range.replace(/bytes=/, "").split("-");
            const chunkStart = parseInt(start, 10);
            const chunkEnd = end ? parseInt(end, 10) : fileSize - 1;

            res.writeHead(206, {
                "Content-Range": `bytes ${chunkStart}-${chunkEnd}/${fileSize}`,
                "Content-Length": chunkEnd - chunkStart + 1,
                "Content-Type": "application/octet-stream",
                "Content-Disposition": `attachment; filename*=UTF-8''${encodedFileName}`
            });

            const fileStream = fs.createReadStream(filePath, { start: chunkStart, end: chunkEnd });
            fileStream.pipe(res);
        } else {
            res.writeHead(200, {
                "Content-Length": fileSize,
                "Content-Type": "application/octet-stream",
                "Content-Disposition": `attachment; filename*=UTF-8''${encodedFileName}`
            });
            fs.createReadStream(filePath).pipe(res);
        }
    } catch (err) {
        console.error("文件错误:", err);
        res.status(500).send("文件下载失败");
    }
});

app.listen(PORT, () => {
    console.log(`服务器运行在 http://localhost:${PORT}`);
});
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>大文件下载</title>
    <style>
      .progress-container {
        width: 100%;
        background-color: #f3f3f3;
        margin-top: 10px;
      }
      .progress-bar {
        width: 0%;
        height: 30px;
        background-color: #4caf50;
        text-align: center;
        line-height: 30px;
        color: #000;
      }
    </style>
  </head>
  <body>
    <h1>大文件下载示例</h1>

    <button id="downloadBtn">下载文件</button>
    <div class="progress-container">
      <div id="progressBar" class="progress-bar">0%</div>
    </div>

    <script>
      document
        .getElementById("downloadBtn")
        .addEventListener("click", async () => {
          const progressBar = document.getElementById("progressBar");
          const url = "http://localhost:3002/download";

          try {
            const response = await fetch(url);
            if (!response.ok) throw new Error("下载失败");

            const contentLength = +response.headers.get("Content-Length");
            let receivedLength = 0;

            const reader = response.body.getReader();
            const chunks = [];

            while (true) {
              const { done, value } = await reader.read();
              if (done) break;

              chunks.push(value);
              receivedLength += value.length;

              // 更新进度条
              const percent = Math.round(
                (receivedLength / contentLength) * 100
              );
              progressBar.style.width = percent + "%";
              progressBar.textContent = percent + "%";
            }

            // 合并所有chunks
            const blob = new Blob(chunks);
            const downloadUrl = URL.createObjectURL(blob);

            // 创建下载链接
            const a = document.createElement("a");
            a.href = downloadUrl;
            a.download = "系统架构师资料.zip";
            document.body.appendChild(a);
            a.click();

            // 清理
            setTimeout(() => {
              document.body.removeChild(a);
              URL.revokeObjectURL(downloadUrl);
            }, 100);
          } catch (error) {
            console.error("下载错误:", error);
            progressBar.style.backgroundColor = "red";
            progressBar.textContent = "下载失败";
          }
        });
    </script>
  </body>
</html>

方案3的效果图
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2383359.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

CSS【详解】弹性布局 flex

适用场景 一维&#xff08;行或列&#xff09;布局 基本概念 包裹所有被布局元素的父元素为容器 所有被布局的元素为项目 项目的排列方向&#xff08;垂直/水平&#xff09;为主轴 与主轴垂直的方向交交叉轴 容器上启用 flex 布局 将容器的 display 样式设置为 flex 或 i…

自回归图像编辑 EditAR: Unified Conditional Generation with Autoregressive Models

Paperhttps://arxiv.org/pdf/2501.04699 Code (coming soon) 目录 方法 实验 EditAR是一个统一的自回归框架&#xff0c;用于各种条件图像生成任务——图像编辑、深度到图像、边缘到图像、分割到图像。 next-token预测的功效尚未被证明用于图像编辑。 EditAR主要构建在Ll…

React Flow 中 Minimap 与 Controls 组件使用指南:交互式小地图与视口控制定制(含代码示例)

本文为《React Agent&#xff1a;从零开始构建 AI 智能体》专栏系列文章。 专栏地址&#xff1a;https://blog.csdn.net/suiyingy/category_12933485.html。项目地址&#xff1a;https://gitee.com/fgai/react-agent&#xff08;含完整代码示​例与实战源&#xff09;。完整介绍…

STM32之串口通信WIFI上云

一、W模块的原理与应用 基本概念 如果打算让硬件设备可以通过云服务器进行通信&#xff08;数据上报/指令下发&#xff09;&#xff0c;像主流的云服务器有阿里云、腾讯云、华为云&#xff0c;以及其他物联网云平台&#xff1a;巴法云.......&#xff0c;硬件设备需要通过TCP…

PCB智能报价系统——————仙盟创梦IDE

软件署名 代码贡献&#xff1a; 紫金电子科技有限公司 文案正路&#xff1a;cybersnow 正文 对企业的竞争力有着深远影响。传统的 PCB 报价方式往往依赖人工核算&#xff0c;不仅耗时较长&#xff0c;还容易出现误差。随着科技的发展&#xff0c;PCB 自动报价系统应运而生&a…

LeetCode-链表-合并两个有序链表

LeetCode-链表-合并两个有序链表 ✏️ 关于专栏&#xff1a;专栏用于记录 prepare for the coding test。 文章目录 LeetCode-链表-合并两个有序链表&#x1f4dd; 合并两个有序链表&#x1f3af;题目描述&#x1f50d; 输入输出示例&#x1f9e9;题目提示&#x1f9ea;AC递归&…

sqli-labs靶场29-31关(http参数污染)

目录 前言 less29&#xff08;单引号http参数污染&#xff09; less30&#xff08;双引号http参数污染&#xff09; less31(双引号括号http参数污染) 前言 在JSP中&#xff0c;使用request.getParameter("id")获取请求参数时&#xff0c;如果存在多个同名参数&a…

JVM 垃圾回收机制深度解析(含图解)

JVM 垃圾回收机制深度解析&#xff08;含图解&#xff09; 一、垃圾回收整体流程 垃圾回收图解 #mermaid-svg-KPtxlwWntQx8TOj3 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-KPtxlwWntQx8TOj3 .error-icon{fill…

如何利用 Conda 安装 Pytorch 教程 ?

如何利用 Conda 安装 Pytorch 教程 &#xff1f; 总共分为六步走&#xff1a; &#xff08;1&#xff09;第一步&#xff1a;验证conda 环境是否安装好&#xff1f; 1) conda -V2) conda --version&#xff08;2&#xff09;第二步&#xff1a;查看现有环境 conda env list…

uniapp vue 开发微信小程序 分包梳理经验总结

嗨&#xff0c;我是小路。今天主要和大家分享的主题是“uniapp vue 开发微信小程序 分包梳理经验总结”。 在使用 UniAppvue框架开发微信小程序时&#xff0c;当项目比较大的时候&#xff0c;经常需要分包加载。它有助于控制主包的大小&#xff0c;从而提升小程序的启…

什么是VR展示?VR展示的用途

随着科技的迅猛发展&#xff0c;我们步入一个全新的数字时代。在这个时代&#xff0c;虚拟现实&#xff08;VR&#xff09;技术崭露头角&#xff0c;逐步改变我们对世界的认知。全景展示厅作为VR技术与传统展览艺术的完美结合&#xff0c;以独特的全景视角&#xff0c;引领我们…

.NET外挂系列:4. harmony 中补丁参数的有趣玩法(上)

一&#xff1a;背景 1. 讲故事 前面几篇我们说完了 harmony 的几个注入点&#xff0c;这篇我们聚焦注入点可接收的几类参数的解读&#xff0c;非常有意思&#xff0c;在.NET高级调试 视角下也是非常重要的&#xff0c;到底是哪些参数&#xff0c;用一张表格整理如下&#xff…

Go语言中new与make的深度解析

在 Go 语言中&#xff0c;new 和 make 是两个用于内存分配的内置函数&#xff0c;但它们的作用和使用场景有显著区别。 理解它们的核心在于&#xff1a; new(T): 为类型 T 分配内存&#xff0c;并将其初始化为零值&#xff0c;然后返回一个指向该内存的指针 (*T)。make(T, ar…

3、ubantu系统 | 通过vscode远程安装并配置anaconda

1、vscode登录 登录后通过pwd可以发现目前位于wangqinag账号下&#xff0c;左侧为属于该账号的文件夹及文件。 通过cd ..可以回到上一级目录&#xff0c;通过ls可以查看当前目录下的文件夹及文件。 2、安装 2.1、下载anaconda 通过wget和curl下载未成功&#xff0c;使用手动…

【Unity】 HTFramework框架(六十五)ScrollList滚动数据列表

更新日期&#xff1a;2025年5月16日。 Github 仓库&#xff1a;https://github.com/SaiTingHu/HTFramework Gitee 仓库&#xff1a;https://gitee.com/SaiTingHu/HTFramework 索引 一、ScrollList滚动数据列表二、使用ScrollList1.快捷创建ScrollList2.ScrollList的属性3.自定义…

Swagger在java的运用

Swagger 是一个广泛使用的工具&#xff0c;用于设计、构建、记录和使用 RESTful Web 服务。它通过提供交互式的 API 文档、客户端 SDK 生成和 API 发现功能&#xff0c;极大地简化了 API 的开发和使用过程。以下是对 Swagger 的详细介绍&#xff0c;包括它的功能、使用场景、如…

代码随想录算法训练营 Day49 图论Ⅰ 深度优先与广度优先

图论 基础 图的概念 图的概念 概念清单有向图 (a)无向图 (b)有向/无向如图 a 所示每条边有指向如图 b 所示每条边没有箭头指向权值每条边的权值每条边的权值度-有几条边连到该节点 (eg V 2 V_2 V2​ 度为 3)入度/出度出度&#xff1a;从该节点出发的边个数入度&#xff1a;…

.NET外挂系列:1. harmony 基本原理和骨架分析

一&#xff1a;背景 1. 讲故事 为什么要开这么一个系列&#xff0c;是因为他可以对 .NET SDK 中的方法进行外挂&#xff0c;这种技术对解决程序的一些疑难杂症特别有用&#xff0c;在.NET高级调试 领域下大显神威&#xff0c;在我的训练营里也是花了一些篇幅来说这个&#xf…

HarmonyOS NEXT端云一体化工程目录结构

视频课程学习报名入口:HarmonyOS NEXT端云一体化开发 端云一体化开发工程由端开发工程(Application)和云开发工程(CloudProgram)两大核心模块构成。 1)端开发工程目录结构 端开发工程主要用于开发应用端侧的业务代码,通用云开发模板的端开发工程目录结构如下图所示: …

Ajax研究

简介 AJAX Asynchronous JavaScript and XML&#xff08;异步的 JavaScript 和 XML&#xff09;。 AJAX 是一种在无需重新加载整个网页的情况下&#xff0c;能够更新部分网页的技术。 Ajax 不是一种新的编程语言&#xff0c;而是一种用于创建更好更快以及交互性更强的Web应用…