Socket
Socket
Sign inDemoInstall

nextjs-chunk-upload-action

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nextjs-chunk-upload-action

Uploading large files with chunking using server action in Next.js


Version published
Weekly downloads
42
increased by27.27%
Maintainers
1
Weekly downloads
 
Created
Source

⭕ nextjs-chunk-upload-action ⭕

Documentation Documentation Documentation Maintenance License: MIT

Uploading large files with chunking using server action in Next.js

📥 Install

npm i nextjs-chunk-upload-action

📖 Example

Example: nextjs-chunk-upload-action-example

// upload-form.tsx

'use client';

import { ChunkUploader } from 'nextjs-chunk-upload-action';

import { chunkUploadAction } from './chunk-upload-action';

export function UploadForm() {
  const handleFormAction = (formData: FormData) => {
    const file = formData.get('file') as File;
    if (!file) return;

    const uploader = new ChunkUploader({
      file,
      onChunkUpload: chunkUploadAction,
      metadata: { name: file.name },
      onChunkComplete: (bytesAccepted, bytesTotal) => {
        console.log('Progress:', `${bytesAccepted} / ${bytesTotal}`);
      },
      onError: error => {
        console.error(error);
      },
      onSuccess: () => {
        console.log('Upload complete');
      },
    });

    uploader.start();
  };

  return (
    <form className="flex flex-col items-center justify-center" action={handleFormAction}>
      <input name="file" type="file" required className="rounded-lg border-2 border-dashed p-4" />
      <button type="submit" className="mt-4 rounded-lg bg-blue-500 p-2 text-white">
        Upload
      </button>
    </form>
  );
}
// chunk-upload-action.ts

'use server';

import type { FileHandle } from 'fs/promises';
import { open } from 'fs/promises';
import { join } from 'path';

import type { ChunkUploadHandler } from 'nextjs-chunk-upload-action';

export const chunkUploadAction: ChunkUploadHandler<{ name: string }> = async (
  chunkFormData,
  metadata
) => {
  const blob = chunkFormData.get('blob');
  const offset = Number(chunkFormData.get('offset'));
  const buffer = Buffer.from(await blob.arrayBuffer());
  const filePath = join('./uploads', metadata.name);

  let fileHandle: FileHandle | undefined;
  try {
    fileHandle = await open(filePath, offset === 0 ? 'w' : 'r+');
    await fileHandle.write(buffer, 0, buffer.length, offset);
  } finally {
    await fileHandle?.close();
  }
};

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

🌟 Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2024 a179346.
This project is MIT licensed.


This README was generated with ❤️ by readme-md-generator

Keywords

FAQs

Package last updated on 01 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc