Cannot upload images with 'multer' and 'next-connect' using 'App Router' #50702
Replies: 2 comments 1 reply
-
|
import dbConnect from "@/app/backend/config/dbConnect"; const router = createRouter(); dbConnect(); const uploadMiddleware = upload.array("image"); } catch (error) { export async function PUT(request, ctx) { i am also not able to get the path from the file it is showing an error {"success":false,"error":{"statusCode":500,"message":"The "path" argument must be of type string or an instance of Buffer or URL. Received undefined","stack":"TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined\n at Object.unlinkSync (node:fs:1805:10)\n at Array.eval (webpack-internal:///(sc_server)/./app/api/auth/me/route.js:55:55)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:249:37)"},"message":"Internal Server Error","stack":"TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined\n at Object.unlinkSync (node:fs:1805:10)\n at Array.eval (webpack-internal:///(sc_server)/./app/api/auth/me/route.js:55:55)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:249:37)"} |
Beta Was this translation helpful? Give feedback.
-
|
In App Router I’d avoid forcing the old For the transport part, Then I’d keep a separate server-side inspection step before the file is stored or forwarded anywhere. Getting a I built an OSS package for that inspection step in Node.js: Minimal shape: import { NextResponse } from 'next/server'
import { scanBytes, STRICT_PUBLIC_UPLOAD } from 'pompelmi'
export const runtime = 'nodejs'
export async function POST(req: Request) {
const form = await req.formData()
const file = form.get('file')
if (!(file instanceof File)) {
return NextResponse.json({ error: 'Missing file' }, { status: 400 })
}
const bytes = Buffer.from(await file.arrayBuffer())
const report = await scanBytes(bytes, {
filename: file.name,
mimeType: file.type,
policy: STRICT_PUBLIC_UPLOAD,
failClosed: true,
})
if (report.verdict !== 'clean') {
return NextResponse.json({ error: 'Rejected upload', report }, { status: 400 })
}
// only now persist or forward the file
return NextResponse.json({ ok: true, verdict: report.verdict })
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The code below does not upload files to my "public/uploads" folder. The objective is to receive an array of files and store them locally with metadata saved in db. The client side sends form data with formdata appended as below. This used to work in
'page router' setup.
"app/api/posts/route.js :"
The client side :
"package.json :"
Beta Was this translation helpful? Give feedback.
All reactions