Problems with making file upload work in app directory
Unanswered
English Angora posted this in #help-forum
English AngoraOP
I am currently having some problems with types, and serializing, specifically when sending file with
formData being formData where values are either string or File.
once i send it to /api/route I take the request object and iterate over it populating fields to formData object which is an object with keys and values
once I populate formData i pass it into main() function that takes form_data argument whose type is "typeof formData"
the main function just creates a new row in a sqlite database:
This approach currently works perfectly for primitive types, but I have no idea how to do this for files
fetch('something/api/route',{body: formData});formData being formData where values are either string or File.
once i send it to /api/route I take the request object and iterate over it populating fields to formData object which is an object with keys and values
(await request.formData()).forEach((value, key) => {
formData[key] = value;
console.log(key, value);
}); once I populate formData i pass it into main() function that takes form_data argument whose type is "typeof formData"
the main function just creates a new row in a sqlite database:
async function main(form_data: typeof formData) {
const form = await prisma.form.create({
data: formData,
});
console.log(form);
}This approach currently works perfectly for primitive types, but I have no idea how to do this for files
3 Replies
English AngoraOP
I tried matching value on formData request, but it can only match with primitive types for some reason
I am also open to using any library for this if needed
English AngoraOP
Okay so I managed to do a
Buffer.from('') to resolve prisma errors, but I still don't know how to take the file (image but I don't think it matters) send it to router handler and then serialize it in binary.