0
import multer from "multer";
export const config = {
    api: {
        bodyParser: false,
    },
};
const upload = multer({ storge: multer.memoryStorage() }).single("file");
export default async function handler(req, res) {
    try {
        upload(req, res, function (err) {
            if (err) {
                console.log("err1", err);
                return res.status(400).json({ success: false, err });
            } else {
                const file = req["file"];
                return res.status(200).json({ success: true, data: file });
            }
        });
    } catch (err) {
        console.log("err2", err);
        return res.status(400).json({ success: false });
    }
}

He always goes to err's end

In nextjs, I just want to write the file to memory and return the buffer of the file, not directly upload it to the folder. However, the multer middleware does not work, and the result is always "req. file===undefined". However, in pure nodejs, I can work as follows: "app. post ('/upload', multer(). single ('file '), async (req, res, err)=>{xxxxxxx}"

0 Answers0