typescriptadvanced
Multipart Upload Stream Handler
Handles multipart file uploads with streaming to disk without buffering the entire file in memory.
typescriptPress ⌘/Ctrl + Shift + C to copy
import { createWriteStream } from 'fs';
import { pipeline } from 'stream/promises';
import { Request, Response } from 'express';
import { randomUUID } from 'crypto';
import path from 'path';
export async function handleUpload(req: Request, res: Response) {
const boundary = req.headers['content-type']?.split('boundary=')[1];
if (!boundary) {
return res.status(400).json({ error: 'Missing boundary' });
}
const filename = `${randomUUID()}${path.extname(req.headers['x-filename'] || '.bin')}`;
const dest = path.join('/tmp/uploads', filename);
try {
await pipeline(req, createWriteStream(dest));
res.json({ file: filename, path: dest });
} catch (err) {
res.status(500).json({ error: 'Upload failed' });
}
}Use Cases
- Large file uploads
- Cloud storage uploads
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptadvanced
JSON Stream Parser
Parses large JSON arrays from a readable stream without loading the entire file into memory.
Best for: Processing large log files
#streaming#json
typescriptadvanced
Partial Prerendering with Suspense
Combine static shells with streamed dynamic content using React Suspense for instant page loads.
Best for: E-commerce product pages
#ppr#suspense
typescriptintermediate
Next.js Streaming with Suspense
Stream server components with Suspense boundaries for progressive page loading and better TTFB.
Best for: Progressive loading for data-heavy dashboards
#nextjs#streaming
pythonintermediate
Generator Pipeline for Data Processing
Chain generators to build memory-efficient data processing pipelines for large files and streams.
Best for: Large file ETL
#generator#pipeline