Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/a179346/nextjs-chunk-upload-action-example

An example of utilizing the nextjs-chunk-upload-action library
https://github.com/a179346/nextjs-chunk-upload-action-example

chunk-upload next-js nextjs-chunk-upload-action server-action

Last synced: 17 days ago
JSON representation

An example of utilizing the nextjs-chunk-upload-action library

Awesome Lists containing this project

README

        

## nextjs-chunk-upload-action-example

> An example of utilizing the [nextjs-chunk-upload-action](https://github.com/a179346/nextjs-chunk-upload-action) library.

## Start

```sh
npm run dev
```

## Usage

```ts
// upload-form.tsx

'use client';

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

import { chunkUploadAction } from '@/server/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 (



Upload


);
}
```

```ts
// 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();
}
};
```