npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

bigfile-chunk-uploader

v1.1.4

Published

支持大文件分片上传的前端组件

Readme

BigFile Chunk Uploader

一个支持大文件分片上传的前端组件,具备断点续传、暂停恢复等功能。

特性

  • ✨ 大文件分片上传
  • 🚀 并发上传控制
  • 💪 断点续传支持
  • ⏯️ 暂停/恢复功能
  • 🔄 自动重试机制
  • 📊 实时进度反馈
  • 🛡️ TypeScript 支持

安装

npm i bigfile-chunk-uploader
# 或者
yarn add bigfile-chunk-uploader

使用示例

基础用法

import { BigFileUploader } from 'bigfile-chunk-uploader';

const uploader = new BigFileUploader({
  file: File,                   // 待上传的文件
  baseURL: 'http://api.example.com',  // API基础URL
  endpoints: {
    init: '/upload/init',       // 初始化上传
    chunk: '/upload/chunk',     // 上传分片
    merge: '/upload/merge',     // 合并分片
    progress: '/upload/progress' // 查询进度
  },
  onProgress: (progress) => {
    console.log(`上传进度: ${progress}%`);
  },
  onSuccess: (response) => {
    console.log('上传成功:', response);
  },
  onError: (error) => {
    console.error('上传失败:', error);
  }
});

// 开始上传
await uploader.start();

高级配置

const uploader = new BigFileUploader({
  // 必需参数
  file: File,
  baseURL: 'http://api.example.com',
  endpoints: {
    init: '/upload/init',
    chunk: '/upload/chunk',
    merge: '/upload/merge',
    progress: '/upload/progress'
  },
  
  // 可选参数
  chunkSize: 5 * 1024 * 1024,  // 分片大小,默认5MB
  concurrent: 3,               // 并发上传数,默认3
  headers: {                   // 自定义请求头
    'Authorization': 'Bearer token'
  },
  withCredentials: true,      // 是否携带认证信息
  maxRetries: 3,             // 最大重试次数
  
  // 回调函数
  onProgress: (progress) => {
    console.log(`上传进度: ${progress}%`);
  },
  onSuccess: (response) => {
    console.log('上传成功:', response);
  },
  onError: (error) => {
    console.error('上传失败:', error);
  },
  onChunkSuccess: (chunkIndex, response) => {
    console.log(`分片 ${chunkIndex} 上传成功`);
  }
});

控制上传过程

// 开始上传
await uploader.start();

// 暂停上传
uploader.pause();

// 继续上传
uploader.resume();

// 中止上传
uploader.abort();

API 文档

构造函数选项

| 参数 | 类型 | 必需 | 默认值 | 说明 | |------|------|------|--------|------| | file | File | 是 | - | 待上传的文件 | | baseURL | string | 是 | - | API基础URL | | endpoints | object | 是 | - | API端点配置 | | chunkSize | number | 否 | 5MB | 分片大小 | | concurrent | number | 否 | 3 | 并发上传数 | | headers | object | 否 | {} | 自定义请求头 | | withCredentials | boolean | 否 | false | 是否携带认证信息 | | maxRetries | number | 否 | 3 | 最大重试次数 | | onProgress | function | 否 | - | 进度回调 | | onSuccess | function | 否 | - | 成功回调 | | onError | function | 否 | - | 错误回调 | | onChunkSuccess | function | 否 | - | 分片上传成功回调 |

实例方法

| 方法 | 说明 | 返回值 | |------|------|--------| | start() | 开始上传 | Promise | | pause() | 暂停上传 | void | | resume() | 继续上传 | void | | abort() | 中止上传 | void |

上传流程说明

完整上传流程

上传流程图

  1. 文件预处理

    • 计算文件 SHA-256 哈希值(占进度 20%)
    • 根据 chunkSize 将文件分片
    • 初始化上传状态
  2. 初始化上传会话

    • 发送 POST /upload/init 请求
    • 携带文件信息和哈希值
    • 获取 uploadId
    • 检查文件是否已存在(秒传)
  3. 分片并发上传

    • 检查已上传的分片(断点续传)
    • 按配置的并发数上传分片
    • 分片上传进度实时反馈(占进度 80%)
    • 失败自动重试
  4. 合并分片

    • 所有分片上传完成后
    • 发送合并请求
    • 获取最终文件 URL

服务端接口要求

通用响应格式:

{
  code: number;     // 状态码,0表示成功
  message: string;     // 错误信息
  result: any        // 结果数据
}

1. 初始化上传 (/upload/init)

请求参数:

{
  fileName: string;     // 文件名
  fileSize: number;     // 文件大小
  chunkSize: number;    // 分片大小
  fileHash: string;     // 文件哈希值
}

result响应格式:

{
  uploadId: string;     // 上传会话ID
  exists?: boolean;     // 文件是否已存在
}

2. 上传分片 (/upload/chunk)

请求格式:

FormData {
  file: Blob;          // 分片数据
  chunkIndex: number;  // 分片索引
  uploadId: string;    // 上传会话ID
  fileHash: string;    // 文件哈希值
}

3. 合并分片 (/upload/merge)

请求参数:

{
  uploadId: string;     // 上传会话ID
  fileHash: string;     // 文件哈希值
  fileName: string;     // 文件名
  totalChunks: number; // 总分片数
}

result响应格式:

{
  fileName: string;     // 文件名称
  fileSize: number;     // 文件大小
  uploadId: string;     // 上传会话标识
  chunkSize: number;     // 分片大小
  totalChunkNum: number;     // 分片数量
  error: string;     // 错误信息
  success: boolean;     // 是否成功
  fileUrl: string;     // 文件上传地址
  startTime: string;     // 文件上传地址
  endTime: string;     // 文件上传地址
}

4. 查询进度 (/upload/progress)

请求参数:

{
  uploadId: string;    // 上传会话ID
}

响应格式:

{
  uploadedChunks: number[];  // 已上传分片索引
  isComplete: boolean;       // 是否已完成
}

Node.js 示例项目

完整的服务端实现示例,包括文件分片存储、合并、断点续传等功能:

  • 示例仓库:big-file-uploader-demo
  • 技术栈:Node.js + Express
  • 功能特性:
    • ✅ 分片上传接口
    • ✅ 秒传判断
    • ✅ 断点续传支持
    • ✅ 分片合并
    • ✅ 临时文件管理
    • ✅ 完整的错误处理