【发布时间】:2021-03-02 20:27:02
【问题描述】:
我正在设置静态路径但出现错误:“错误:ENOENT:没有这样的文件或目录,打开 'C:\dashboard new - Copy\uploads\2019-11-28T08:11:09.164Z1564660431900.jpg'”
const storage = multer.diskStorage({ destination: function(req, file, cb) { let dest = path.join(__dirname, '../../uploads'); cb(null, dest); }, filename: function(req, file, cb) { cb(null, new Date().toISOString() + file.originalname); }});
const fileFilter = (req, file, cb) => { if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') { cb(null, true); } else { cb(null, false); }};
const upload = multer({ storage: storage, limits: { fileSize: 1024 * 1024 * 5 }, fileFilter: fileFilter});
router.post("/", upload.single('productImage'), async (req, res, next) => {
try {
cosole.log('hi');
const product = new Product({
_id: new mongoose.Types.ObjectId(),
name: req.body.name,
price: req.body.price,
productImage: req.file.path
});
const saveImage = await product.save();
console.log(saveImage)
res.json(saveImage);
} catch (error) {
console.log(error);
res.json(error);
}
});
如何做到这一点?
【问题讨论】:
-
我不确定它是否会起作用,如果您使用的是 express,那么也尝试提供类似
app.use('/static', express.static(path.join(__dirname, 'your-folder-path')))或app.use(express.static('public'))之类的静态文件夹, -
我想你看过“Academind”的视频吧?我也是!我认为当我们加入“../../upload”格式的路径时,你必须通过正确的路径