Answers for "nextjs accept file"

0

nextjs accept file

//form
<form method="post" action="/api/new" enctype="multipart/form-data">
<input name="logo" type="file" />  
</form>
//install these packages
npm install next-connect multiparty

//middleware module
import nextConnect from 'next-connect'
import multiparty from 'multiparty'

const middleware = nextConnect()

middleware.use(async (req, res, next) => {
  const form = new multiparty.Form()

  await form.parse(req, function (err, fields, files) {
    req.body = fields
    req.files = files
    next()
  })
})

export default middleware

//api/uploadcsv
import middleware from 'middleware/middleware'
import nextConnect from 'next-connect'

const handler = nextConnect()
handler.use(middleware)

handler.post(async (req, res) => {
  console.log(req.body)
  console.log(req.files)

  //...
})

export const config = {
  api: {
    bodyParser: false
  }
}

export default handler
Posted by: Guest on October-09-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language