Archerist
Archerist
NNuxt
Created by Archerist on 7/25/2024 in #❓・help
Reading multiple files using readFormData
i have an event handler that accepts post requests like this:
export default defineEventHandler(async (event) => {
let body = await readFormData(event)
let audioFile = body.get("audio") as File

console.log(body)
let otherFiles = body.get("files[]")
let otherFiles2 = body.get("files[]")
console.log(otherFiles)
console.log(otherFiles2)

})
export default defineEventHandler(async (event) => {
let body = await readFormData(event)
let audioFile = body.get("audio") as File

console.log(body)
let otherFiles = body.get("files[]")
let otherFiles2 = body.get("files[]")
console.log(otherFiles)
console.log(otherFiles2)

})
In the first log i can see that the files[] is an array
FormData {
'files[]':
[ File { size: 165500739,
type: 'application/zip',
name: 'Archive.zip',
lastModified: 1721897215312 },
File { size: 165500739,
type: 'application/zip',
name: 'Archive(1).zip',
lastModified: 1721897215379 },
File { size: 23793,
type: 'application/pdf',
name: 'Untitled document.pdf',
lastModified: 1721897215379 },
File { size: 87902,
type: 'image/jpeg',
name: 'katalog_icon.jpg',
lastModified: 1721897215379 } ],
audio:
File { size: 6738,
type: 'audio/webm;codecs=opus',
name: 'audio.webm',
lastModified: 1721897215380 } }
FormData {
'files[]':
[ File { size: 165500739,
type: 'application/zip',
name: 'Archive.zip',
lastModified: 1721897215312 },
File { size: 165500739,
type: 'application/zip',
name: 'Archive(1).zip',
lastModified: 1721897215379 },
File { size: 23793,
type: 'application/pdf',
name: 'Untitled document.pdf',
lastModified: 1721897215379 },
File { size: 87902,
type: 'image/jpeg',
name: 'katalog_icon.jpg',
lastModified: 1721897215379 } ],
audio:
File { size: 6738,
type: 'audio/webm;codecs=opus',
name: 'audio.webm',
lastModified: 1721897215380 } }
but when i use body.get("files[]") ,I'm only getting the first file in the array.
File { size: 165500739,
type: 'application/zip',
name: 'Archive.zip',
lastModified: 1721897215312 }
File { size: 165500739,
type: 'application/zip',
name: 'Archive.zip',
lastModified: 1721897215312 }
Do i use readMultipartFormData here instead? I'm not really clear on how to use it properly so help is appreciated.
3 replies