0

Find the reason why I can't save files on the server (But localhost saves ) images to the /public/uploads folder, it exists and its access rights are drwxrwxrwx. There are no errors, the api writes that everything is successful, it saves the file name to the database, but there are no files in the /public/uploads folder itself.

My configuration : next.js - prisma

My api (next-connect):

const upload = multer({
    storage: multer.diskStorage({
        destination: './public/uploads',
        filename: (req, file, cb) => cb(null, file.originalname),
    }),
});

const apiRoute = nextConnect<NextApiRequest & { file?: Express.Multer.File }, NextApiResponse>({
    onError(error, req, res) {
        res.status(501).json({ error: `Sorry something Happened! ${error.message}` });
    },
    onNoMatch(req, res) {
        res.status(405).json({ error: `Method '${req.method}' Not Allowed` });
    },
});

apiRoute.use(upload.single('image'));
apiRoute.use(helmet());

apiRoute.post(async (req, res, next) => {
    const token = req.cookies['sid']
    const admin = await checkSession(token)
    if (admin) {
        return next()
    }
    next(new Error('Auth required'))
}, async (req, res) => {
    try {
        const img: string = req.file ? req.file.filename : ''
        const loginSchema = z.object({
            title: z.string().min(2).max(50),
            shortDesc: z.string().min(2).max(150),
            description: z.string(),
            filterMainPeople: z.string().min(2).max(60),
            detailFilterBrand: z.string().min(2).max(60),
            detailFilterMode: z.string().min(2).max(60),
            price: z.string().max(60),
        })
        const {
            title, shortDesc, description, filterMainPeople, detailFilterBrand, detailFilterMode, price
        } = loginSchema.parse(req.body)
        const newOffer = await db.offer.create({
            data: {
                title,
                shortDesc,
                description,
                filterMainPeople,
                detailFilterBrand,
                detailFilterMode,
                img,
                price,
                active: true,
            }
        })
        res.status(200).send(newOffer)
    } catch (error) {
        console.error(error)
        if (error instanceof ZodError) {
            return res.status(404).send({ message: "validation error" })
        }
        res.status(500).send({ message: "server error" })
    }
});

export default apiRoute;

export const config = {
    api: {
        bodyParser: false, // Disallow body parsing, consume as stream
    },
};
2
  • 1
    Vercel won't let you save files out of the box in the public folder of Next.js. Check out vercel.com/guides/how-to-upload-and-store-files-with-vercel. Commented May 18, 2023 at 8:01
  • @youssouf-oumar thanks. Is there a way to save it to the my server with Next simply , but to mine in any way , and then , for example , I run the build ? Are there any workarounds, save the file to the server, then just call the build of the project and so that it would work? I see this stackoverflow.com/questions/60465564/… its work? Commented May 19, 2023 at 7:12

1 Answer 1

0

I assumed the same when the application was deployed on the server using Nginx the file was not getting saved, but unfortunately the file was getting saved in a different path in the Linux server.

Understand if the file, you are uploading is getting saved in the server using the below cmd - "find -type f -name Filename with extension"

Example: find -type f -name test.txt

If the file was saved in a different path, please change the configuration. I resolved the issue!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.