Created
February 12, 2021 03:06
-
-
Save konojunya/b337a4e048b5dac4f9385b5a0cfd7c62 to your computer and use it in GitHub Desktop.
Next.js file uploader with busboy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NextApiRequest, NextApiResponse } from 'next'; | |
import Busboy from 'busboy'; | |
import { inspect } from 'util'; | |
export const config = { | |
api: { | |
bodyParser: false, | |
}, | |
}; | |
async function r(req: NextApiRequest) { | |
return new Promise((resolve) => { | |
const busboy = new Busboy({ headers: req.headers }); | |
console.log(JSON.stringify(req.headers, null, 2)); | |
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) { | |
console.log( | |
'File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype, | |
); | |
file.on('data', function (data) { | |
console.log('File [' + fieldname + '] got ' + data.length + ' bytes'); | |
}); | |
file.on('end', function () { | |
console.log('File [' + fieldname + '] Finished'); | |
}); | |
}); | |
busboy.on('field', function (fieldname, val) { | |
console.log('Field [' + fieldname + ']: value: ' + inspect(val)); | |
}); | |
busboy.on('finish', function () { | |
console.log('Done parsing form!'); | |
resolve(1); | |
}); | |
req.pipe(busboy); | |
}); | |
} | |
export default async function imageUploadHandler(req: NextApiRequest, res: NextApiResponse) { | |
if (req.method !== 'POST') { | |
return res.status(405).end(); | |
} | |
await r(req); | |
res.status(200).end(JSON.stringify({ ok: true })); | |
} |
gurvirbaraich
commented
Aug 24, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment