How can I upload multiple files as input? I couldn’t find a solution, and when I tried sending them as base64, the request became too large #627
BirajMainali
started this conversation in
General
Replies: 2 comments
-
|
Hey! I'm also trying to parse multipart/form-data in a request handler but can't find a proper solution for this, did you find any solution for this? |
Beta Was this translation helpful? Give feedback.
0 replies
-
import { ApiRouteConfig, Handlers } from 'motia'
import { z } from 'zod'
import multer from 'multer'
// Configure multer for file uploads (memory storage)
const upload = multer({ storage: multer.memoryStorage() })
export const config: ApiRouteConfig = {
type: 'api',
name: 'document-ingest-api',
description: 'API trigger for document ingestion',
flows: ['document-ingest'],
method: 'POST',
path: '/api/ingest',
bodyParser: false, // IMPORTANT → disable Motia JSON parser for form-data
middleware: [upload.array('files')], // Accept multiple files named "files"
bodySchema: z.object({
tenantId: z.string().uuid(),
secret: z.string()
}),
responseSchema: {
200: z.object({
message: z.string(),
processedFiles: z.number(),
}),
},
emits: ['process-file-ingest'],
}
export const handler: Handlers['document-ingest-api'] = async (req, { logger, emit }) => {
const { tenantId, secret } = req.body
const files = req.files as Express.Multer.File[]
logger.info("Step:1 - Received document ingest request", {
tenantId,
fileCount: files.length
})
// Emit ingestion events per file
for (const file of files) {
await emit('process-file-ingest', {
tenantId,
fileName: file.originalname,
mimeType: file.mimetype,
buffer: file.buffer,
})
}
return {
status: 200,
body: {
message: "Document ingestion successful",
processedFiles: files.length,
}
}
}@BirajMainali Hey buddy check this out for a sec. Use multer like this for uploading files |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Beta Was this translation helpful? Give feedback.
All reactions