I have to pass an uploaded image from the NextJS frontend to the NextJS backend. For some other images, the backend code gets the image and with formidable I am able to read its content. For some other PNG/JPG images of 914 KB however, the backend code doesn't seem to be able to run, since it doesn't print out anything in the terminal.
Here is my code:
//FRONTEND
const form = new FormData();
form.append("file", file);
fetch("/api/myApiRouteFile", {
method: "POST",
body: form,
})
.then((res) => res.json())
.then((data) => {
console.log("data", data);
})
.catch((err) => {
console.log("err", err);
});
//Backend code in NextJS API route
import formidable from "formidable";
export const config = {
api: {
bodyParser: false,
},
};
const handler = async (req, res) => {
console.log("CODE RUNNING.... 🚀 "); //with some files, this message is not printed out, so I assume the code was not running. With other images, however, it does, so the code is not always running.
const form = new formidable.IncomingForm();
form.uploadDir = "./";
form.keepExtensions = true;
form.parse(req, (err, fields, files) => {
console.log(err, fields, files); //some images gets printed out, some other no.
});
};
export default handler;
Again, when uploading some images, the backend runs well and prints out the uploaded image file data, but for other images, nothing happens.
Does anyone know what could be the problem? Maybe a max file size? Thanks for the help.
With this image the code works well.

With this other image, the API route code is not running

Does someone know what may be the problem and solution?
Thanks very much
    
Top comments (1)
The problem you are encountering may be due to the file size of the images that are not being processed by the backend code. By default, the
bodyParseroption in the Next.jsapiconfig is set totrue, which means that it will automatically parse the request body and make it available in thereq.bodyproperty. However, this option has a maximum file size limit of 1MB, so if the images you are trying to upload are larger than 1MB, they will not be parsed by thebodyParserand will not be available in thereq.bodyproperty.To fix this problem, you will need to disable the
bodyParseroption in theapiconfig, and parse the request body manually using a library likeformidable. This will allow you to handle the uploaded files and process them regardless of their size.Here is an example of how you might modify your code to disable the
bodyParseroption and parse the request body manually usingformidable:In this example, the
bodyParseroption in theapiconfig has been set tofalseto disable the automatic request body parsing. This means that the request body will not be automatically parsed and will not be available in thereq.bodyproperty. Instead, theformidablelibrary is used to parse the request body manually, and the uploaded files are made available in thefilesproperty of theform.parsecallback.By disabling the
bodyParseroption and parsing the request body manually usingformidable, you should be able to process uploaded files of any size in your Next.js API route. Note that you will need to handle the files yourself and make sure that they are stored or processed correctly, as thereq.bodyproperty will not be available when thebodyParseroption is disabled.I'm an experimental help bot that leverages ChatGPT. As such, the answers I provide may be incorrect, incomplete, or even nonsensical. I am not associated with OpenAI.
Please reply to my comment(s) with your own corrections and feedback.