मुख्य सामग्री पर जाएं

खंडित अपलोड

बड़ी फ़ाइलों को छोटे खंडों में विभाजित करके अपलोड करें। यह सक्षम करता है:

  • सर्वर सीमा से बड़ी फ़ाइलों का अपलोड
  • नेटवर्क विफलताओं के बाद पुनः शुरू करने योग्य अपलोड
  • बेहतर प्रगति ट्रैकिंग
  • कम मेमोरी उपयोग

बुनियादी खंडित अपलोड

import { useDropup, createChunkedUploader } from '@samithahansaka/dropup';

function ChunkedUploader() {
const { files, actions, state, getDropProps, getInputProps } = useDropup({
upload: createChunkedUploader({
url: '/api/upload/chunk',
chunkSize: 5 * 1024 * 1024, // 5MB खंड
}),
});

return (
<div>
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>यहाँ बड़ी फ़ाइलें छोड़ें - वे खंडों में अपलोड होंगी</p>
</div>

{files.map(file => (
<div key={file.id} style={styles.fileItem}>
<span>{file.name}</span>
<span>{(file.size / 1024 / 1024).toFixed(1)} MB</span>

{file.status === 'uploading' && (
<div style={styles.progressBar}>
<div
style={{
...styles.progress,
width: `${file.progress}%`,
}}
/>
</div>
)}

<span>{file.status}</span>
</div>
))}

<button
onClick={() => actions.upload()}
disabled={state.isUploading}
>
अपलोड करें
</button>
</div>
);
}

const styles = {
dropzone: {
border: '2px dashed #ccc',
borderRadius: 8,
padding: 40,
textAlign: 'center' as const,
marginBottom: 20,
},
fileItem: {
display: 'flex',
alignItems: 'center',
gap: 12,
padding: 12,
borderBottom: '1px solid #eee',
},
progressBar: {
flex: 1,
height: 8,
backgroundColor: '#eee',
borderRadius: 4,
overflow: 'hidden',
},
progress: {
height: '100%',
backgroundColor: '#4caf50',
transition: 'width 0.2s',
},
};

खंडित अपलोड विकल्प

createChunkedUploader({
// आवश्यक
url: '/api/upload/chunk',

// वैकल्पिक सेटिंग्स
chunkSize: 5 * 1024 * 1024, // 5MB (डिफ़ॉल्ट)
concurrency: 3, // समानांतर खंड (डिफ़ॉल्ट: 1)
retries: 3, // विफल खंडों को पुनः प्रयास करें (डिफ़ॉल्ट: 3)

// सभी खंड अनुरोधों के लिए हेडर
headers: {
'Authorization': 'Bearer token',
},

// कस्टम खंड मेटाडेटा
getChunkMeta: (file, chunkIndex, totalChunks) => ({
fileId: file.id,
fileName: file.name,
chunkIndex,
totalChunks,
}),
});

सर्वर-साइड हैंडलिंग

आपके सर्वर को खंड अपलोड को संभालने और उन्हें फिर से जोड़ने की आवश्यकता है।

उदाहरण: Node.js/Express

const express = require('express');
const multer = require('multer');
const fs = require('fs');
const path = require('path');

const app = express();
const upload = multer({ dest: 'chunks/' });

const uploadState = new Map();

app.post('/api/upload/chunk', upload.single('chunk'), (req, res) => {
const { fileId, fileName, chunkIndex, totalChunks } = req.body;

// खंड जानकारी संग्रहीत करें
if (!uploadState.has(fileId)) {
uploadState.set(fileId, {
fileName,
totalChunks: parseInt(totalChunks),
chunks: [],
});
}

const state = uploadState.get(fileId);
state.chunks.push({
index: parseInt(chunkIndex),
path: req.file.path,
});

// जांचें कि सभी खंड प्राप्त हुए हैं या नहीं
if (state.chunks.length === state.totalChunks) {
// इंडेक्स के अनुसार खंडों को क्रमबद्ध करें
state.chunks.sort((a, b) => a.index - b.index);

// खंडों को संयोजित करें
const finalPath = path.join('uploads', fileName);
const writeStream = fs.createWriteStream(finalPath);

for (const chunk of state.chunks) {
const data = fs.readFileSync(chunk.path);
writeStream.write(data);
fs.unlinkSync(chunk.path); // खंड साफ़ करें
}

writeStream.end();
uploadState.delete(fileId);

return res.json({
complete: true,
url: `/uploads/${fileName}`,
});
}

res.json({
complete: false,
received: state.chunks.length,
total: state.totalChunks,
});
});

tus प्रोटोकॉल

मजबूत पुनः शुरू करने योग्य अपलोड के लिए, tus प्रोटोकॉल का उपयोग करें:

import { useDropup } from '@samithahansaka/dropup';
import { createTusUploader } from '@samithahansaka/dropup/tus';

function TusUploader() {
const { files, actions, state, getDropProps, getInputProps } = useDropup({
upload: createTusUploader({
endpoint: 'https://tusd.tusdemo.net/files/',

// वैकल्पिक सेटिंग्स
chunkSize: 5 * 1024 * 1024,
retryDelays: [0, 1000, 3000, 5000],

// सर्वर के लिए मेटाडेटा
metadata: {
filetype: 'file.type',
filename: 'file.name',
},

// localStorage से पुनः शुरू करें
storeFingerprintForResuming: true,
}),

onUploadComplete: (file) => {
console.log('tus अपलोड पूर्ण:', file.uploadedUrl);
},
});

return (
<div>
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>पुनः शुरू करने योग्य अपलोड के लिए फ़ाइलें छोड़ें</p>
</div>

{files.map(file => (
<div key={file.id} style={styles.fileItem}>
<span>{file.name}</span>
<span>{file.progress}%</span>
<span>{file.status}</span>
</div>
))}

<button onClick={() => actions.upload()}>
अपलोड करें
</button>
</div>
);
}

रोकें और पुनः शुरू करें

खंडित अपलोड के साथ, आप रोक और पुनः शुरू कर सकते हैं:

function PausableUploader() {
const { files, actions, getDropProps, getInputProps } = useDropup({
upload: createChunkedUploader({
url: '/api/upload/chunk',
}),
});

return (
<div>
<div {...getDropProps()}>
<input {...getInputProps()} />
<p>यहाँ फ़ाइलें छोड़ें</p>
</div>

{files.map(file => (
<div key={file.id}>
<span>{file.name}</span>
<span>{file.progress}%</span>
<span>{file.status}</span>

{file.status === 'uploading' && (
<button onClick={() => actions.cancel(file.id)}>
रोकें
</button>
)}

{file.status === 'paused' && (
<button onClick={() => actions.retry([file.id])}>
पुनः शुरू करें
</button>
)}
</div>
))}

<button onClick={() => actions.upload()}>
सभी शुरू करें
</button>
</div>
);
}

खंडों के साथ प्रगति ट्रैकिंग

import { useState } from 'react';

function DetailedChunkProgress() {
const [chunkProgress, setChunkProgress] = useState<Map<string, number[]>>(
new Map()
);

const { files, actions, getDropProps, getInputProps } = useDropup({
upload: createChunkedUploader({
url: '/api/upload/chunk',
chunkSize: 1024 * 1024, // दृश्यता के लिए 1MB खंड

onChunkProgress: (file, chunkIndex, progress) => {
setChunkProgress(prev => {
const next = new Map(prev);
const fileProgress = next.get(file.id) || [];
fileProgress[chunkIndex] = progress;
next.set(file.id, fileProgress);
return next;
});
},
}),
});

return (
<div>
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>यहाँ फ़ाइलें छोड़ें</p>
</div>

{files.map(file => {
const chunks = chunkProgress.get(file.id) || [];
const totalChunks = Math.ceil(file.size / (1024 * 1024));

return (
<div key={file.id} style={styles.fileCard}>
<p>{file.name}</p>

{/* खंड प्रगति विज़ुअलाइज़ेशन */}
<div style={styles.chunkGrid}>
{Array.from({ length: totalChunks }).map((_, i) => (
<div
key={i}
style={{
...styles.chunkBlock,
backgroundColor: chunks[i] === 100
? '#4caf50'
: chunks[i] > 0
? '#8bc34a'
: '#eee',
}}
title={`खंड ${i + 1}: ${chunks[i] || 0}%`}
/>
))}
</div>

<p>समग्र: {file.progress}%</p>
</div>
);
})}

<button onClick={() => actions.upload()}>
अपलोड करें
</button>
</div>
);
}

const styles = {
dropzone: {
border: '2px dashed #ccc',
padding: 40,
textAlign: 'center' as const,
marginBottom: 20,
},
fileCard: {
padding: 16,
border: '1px solid #eee',
borderRadius: 8,
marginBottom: 12,
},
chunkGrid: {
display: 'flex',
flexWrap: 'wrap' as const,
gap: 4,
margin: '12px 0',
},
chunkBlock: {
width: 20,
height: 20,
borderRadius: 4,
transition: 'background-color 0.2s',
},
};

खंड आकार सिफारिशें

फ़ाइल आकारअनुशंसित खंड आकार
< 10 MBखंड की आवश्यकता नहीं
10-100 MB5 MB
100 MB - 1 GB10 MB
> 1 GB20-50 MB
प्रदर्शन

बड़े खंड = कम अनुरोध, लेकिन विफलता पर लंबी रिकवरी। छोटे खंड = अधिक ओवरहेड, लेकिन बेहतर पुनः शुरू करने की क्षमता।