0

I am setting static path but getting error : "Error: ENOENT: no such file or directory, open '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);
}

});

How to do this?

2
  • I am not sure , either it will work or not, if you are using express then also try to serve static folder like this app.use('/static', express.static(path.join(__dirname, 'your-folder-path'))) or app.use(express.static('public')) something like that , Commented Nov 28, 2019 at 8:39
  • I think you watch the video of "Academind" right? me too! I think when we join the path the format "../../upload" not make work you have to pass the right path Commented Mar 2, 2021 at 12:27

1 Answer 1

2

I think you need to provide the destination folder as a key and value, something like this(below)

var upload = multer({ dest: 'uploads/' })

You can check out the full multer documentations here https://expressjs.com/en/resources/middleware/multer.html

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.