找到我无法在服务器上保存文件(但localhost保存)图像到/public/uploads文件夹的原因,它存在并且其访问权限是drwxrwxrwx。没有错误,api写的一切都成功了,它把文件名保存到数据库中,但是 /public/uploads 文件夹本身没有文件。
我的配置:next.js - prisma
我的 api(下一个连接):
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
},
};