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 🙏

© 2026 – Pkg Stats / Ryan Hefner

upload-chunkfile

v0.2.11

Published

This is a file upload library as chunk size.

Readme

Upload Chunkfile

A robust and customizable JavaScript class for uploading large files in chunks. This library is designed for seamless integration into web applications that require efficient file uploads with progress tracking and resumable uploads.

  1. Features
  2. Benefits
  3. Installation
  4. Usage
  5. Configuration Options
  6. How It Works
  7. FAQ

Features

  • Chunked Uploads: Split large files into smaller chunks for reliable uploads.
  • Customizable Chunk Size: Set your preferred chunk size based on network conditions.
  • Abortable Uploads: Cancel ongoing uploads using AbortController.
  • Progress Tracking: Real-time progress updates for a better user experience.
  • Flexible HTTP Methods: Support for POST, PUT, or any HTTP method.
  • Multi-File and Single-File Upload Modes: Adaptable to different use cases.

Benefits

  1. Reliable Large File Uploads: Handles large files without overwhelming network resources.
  2. Resumable Uploads: Easily implement resumable uploads (with additional backend support).
  3. Progress Feedback: Keeps users informed about upload progress.
  4. Network Efficiency: Avoids uploading the entire file again if an error occurs mid-transfer.
  5. Customizable: Highly adaptable to different server requirements and APIs.

Installation

Upload-chunkfile already published on npm.

npm install upload-chunkfile
# or
yarn add upload-chunkfile
# or
pnpm add upload-chunkfile
# or
bun add upload-chunkfile

Usage

Basic Usage

You can integrate this library by importing it into your project:

import UploadChunkFile from "upload-chunkfile";

Here’s a simple example of how to use UploadChunkFile to upload a file:

const uploader = new UploadChunkFile({
  uploadType: "multiple", // Use 'single' for single-chunk uploads. DEFAULT: 'multiple'
  method: "POST", // DEFAULT: 'POST'
  chunkSize: 2 * 1024 * 1024, // 2MB chunk size. DEFAULT: 5MB
});

const file = document.querySelector("#fileInput").files[0]; // Select a file input element

uploader
  .uploadFile({
    file,
    uploadUrl: "https://your-server.com/upload",
    onProgressChange: (progress) => {
      console.log(`Upload Progress: ${progress.toFixed(2)}%`);
    },
  })
  .then((response) => {
    console.log("Upload completed successfully:", response);
  })
  .catch((error) => {
    console.error("Upload failed:", error);
  });

Abort Upload

const abortController = new AbortController();
const uploader = new UploadChunkFile(
  {
    /* Options */
  },
  abortController.signal
);

setTimeout(() => {
  abortController.abort();
  console.log("Upload aborted");
}, 5000); // Abort after 5 seconds

React Usage

const [file, setFile] = (useState < File) | (null > null);
const [progress, setProgress] = useState(0);

const handleUpload = useCallback(async () => {
  const uploadChunkFile = new UploadChunkFile({
    maxParallel: 1,
    retryDelay: 100,
    payloadOptions: {
      chunkName: "file",
    },
  });

  if (file) {
    try {
      const res = await uploadChunkFile.uploadFile({
        file,
        uploadUrl: "http://127.0.0.1:8000/api/upload",
        onProgressChange: (progress) => {
          setProgress(progress);
        },
      });
      console.log(res);
    } catch (error) {
      console.error(error);
    }
  }
}, [file]);

Configuration Options

UploadChunkFile take two parameters, first one is options object and second one is AbortSignal

  1. options parameter value.

| Option | Type | Default | Description | | ------------- | -------- | ----------------- | ----------------------------------------------------------------- | | method | string | 'POST' | HTTP method used for the upload. 'POST' 'PUT' 'PATCH' | | uploadType | string | 'multiple' | 'single' for single upload or 'multiple' for chunked uploads. | | chunkSize | number | 5 * 1024 * 1024 | Size of the each chunk | | maxRetries | number | 2 | How many time the request retry if failed | | retryDelay | number | 1000 | Delay of each request retry | | maxParallel | number | 1 | How many request will send as parallelly |

  1. options can take payloadOptions value that can be used to modify the payload options.

| Option | Type | Default | Description | | -------------- | -------- | -------------- | ---------------------------------------------- | | chunkName | string | chunk | This is each chunk name in the payload | | fileName | string | fileName | This is each file name in the payload | | currentChunk | string | currentChunk | This is each current chunk name in the payload | | totalChunk | string | totalChunk | This is total chunk name in the payload |

  1. uploadChunkFile.uploadFile parameter value.

| Option | Type | Default | Description | | ------------------ | ---------------------------- | ------- | -------------------------------------- | | file | File | null | This is the file | | uploadUrl | string | null | This is the upload url | | onProgressChange | (progress: number) => void | null | This is the progress callback function |

How It Works

  1. File Splitting: The file is divided into chunks based on the specified chunkSize.
  2. Sequential Uploads: Each chunk is uploaded sequentially to the server.
  3. Error Handling: If an error occurs during a chunk upload, the process can be retried (requires additional implementation).
  4. Progress Reporting: The progress is calculated and reported using the onProgressChange callback.

Faq

  1. How do I handle resumable uploads? You can implement resumable uploads by adding metadata to each chunk (e.g., chunk index) and handling them on the server side.

  2. What happens if a chunk fails to upload? The uploadFile method rejects the promise if a chunk fails to upload. You can implement retry logic in your application.

  3. Can I use this with any backend? Yes! The package is backend-agnostic. As long as your backend supports receiving file chunks, it will work seamlessly.