การอัปโหลดแบบแบ่งชิ้น
อัปโหลดไฟล์ขนาดใหญ่โดยแบ่งเป็นชิ้นเล็กๆ ซึ่งช่วยให้:
- อัปโหลดไฟล์ที่ใหญ่กว่าขีดจำกัดของเซิร์ฟเวอร์
- อัปโหลดแบบ resumable หลังจากเครือข่ายล้มเหลว
- ติดตามความคืบหน้าได้ดีขึ้น
- ใช้หน่วยความจำน้อยลง
การอัปโหลดแบบแบ่งชิ้นพื้นฐาน
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 สำหรับทุก request ของ chunk
headers: {
'Authorization': 'Bearer token',
},
// Metadata ของ chunk แบบกำหนดเอง
getChunkMeta: (file, chunkIndex, totalChunks) => ({
fileId: file.id,
fileName: file.name,
chunkIndex,
totalChunks,
}),
});
การจัดการฝั่งเซิร์ฟเวอร์
เซิร์ฟเวอร์ของคุณต้องจัดการการอัปโหลด chunk และรวมกลับ
ตัวอย่าง: 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;
// เก็บข้อมูล chunk
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,
});
// ตรวจสอบว่าได้รับ chunk ทั้งหมดแล้ว
if (state.chunks.length === state.totalChunks) {
// เรียงลำดับ chunk ตาม index
state.chunks.sort((a, b) => a.index - b.index);
// รวม chunks
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); // ล้าง chunk
}
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 Protocol
สำหรับการอัปโหลดแบบ resumable ที่แข็งแกร่ง ใช้ tus protocol:
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 สำหรับเซิร์ฟเวอร์
metadata: {
filetype: 'file.type',
filename: 'file.name',
},
// Resume จาก localStorage
storeFingerprintForResuming: true,
}),
onUploadComplete: (file) => {
console.log('อัปโหลด tus สำเร็จ:', file.uploadedUrl);
},
});
return (
<div>
<div {...getDropProps()} style={styles.dropzone}>
<input {...getInputProps()} />
<p>วางไฟล์สำหรับการอัปโหลดแบบ resumable</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>
);
}
การติดตามความคืบหน้าพร้อม Chunks
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>
{/* การแสดงผลความคืบหน้า chunk */}
<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={`Chunk ${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',
},
};
คำแนะนำขนาด Chunk
| ขนาดไฟล์ | ขนาด Chunk ที่แนะนำ |
|---|---|
| < 10 MB | ไม่จำเป็นต้องแบ่ง |
| 10-100 MB | 5 MB |
| 100 MB - 1 GB | 10 MB |
| > 1 GB | 20-50 MB |
ประสิทธิภาพ
Chunk ขนาดใหญ่ = request น้อยลง แต่การกู้คืนนานขึ้นเมื่อล้มเหลว Chunk ขนาดเล็ก = overhead มากขึ้น แต่ resumability ดีขึ้น