You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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. what am I missing?
"app/api/posts/route.js :"
import{createEdgeRouter}from'next-connect';import{NextResponse}from'next/server';importmulterfrom'multer';importdbfrom'../../../utils/db';importProductfrom'../../../models/Product';constrouter=createEdgeRouter();// Set The Storage Engineconststorage=multer.diskStorage({destination: './public/uploads',filename: function(req,file,cb){console.log('inside multer storage callback',file);cb(null,file.fieldname+'-'+Date.now()+path.extname(file.originalname));},});// Init Uploadconstupload=multer({storage: storage,limits: {fileSize: 700000},//700K filesizefileFilter: function(req,file,cb){checkFileType(file,cb);},});// Check File TypefunctioncheckFileType(file,cb){console.log('Inside checkFileType',file);// Allowed extconstfiletypes=/jpeg|jpg|png|gif|webp|svg/;// Check extconstextname=filetypes.test(path.extname(file.originalname).toLowerCase());// Check mimeconstmimetype=filetypes.test(file.mimetype);if(mimetype&&extname){returncb(null,true);}else{cb('Error: Images Only!');}}//"images" is formdata.append name from client side.router.post(upload.array('images'),async(req)=>{constform=awaitreq.formData();try{constfiles=form.get('images');constfileLocation=files.map((file)=>`/uploads/${file.filename}`);// create an array of local file destination to be saved in DB along with MetadataconstproductMetaData={country: form.get('country'),state: form.get('state'),city: form.get('city'),imagelocation: fileLocation,};awaitdb.connect();constproduct=newProduct(productMetaData);awaitproduct.save();returnNextResponse.json({message: 'Product created successfully'});}catch(e){constres=NextResponse.json({error: e.message},{status: 500});returnres;}})exportasyncfunctionPOST(req){returnrouter.run(req);}exportconstconfig={api: {externalResolver: true,bodyParser: false,// Disallow body parsing, consume as stream; also for multer to do the parsing for dataForm},};
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
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. what am I missing?
"app/api/posts/route.js :"
The client side :
"package.json :"
Beta Was this translation helpful? Give feedback.
All reactions