Skip to content

Instantly share code, notes, and snippets.

@konojunya
Created February 12, 2021 03:06
Show Gist options
  • Save konojunya/b337a4e048b5dac4f9385b5a0cfd7c62 to your computer and use it in GitHub Desktop.
Save konojunya/b337a4e048b5dac4f9385b5a0cfd7c62 to your computer and use it in GitHub Desktop.
Next.js file uploader with busboy
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
Copy link

export const POST = async function (request: NextRequest) {
  const status = await upload(request);

  return NextResponse.json({
    status,
  });
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment