Home > Software engineering >  Multer Doesn't Save Images in Local Folder
Multer Doesn't Save Images in Local Folder

Time:09-25

Multer cannot store the file, where the destination is in public/my-uploads

  const express = require('express');
        const app = express();
        const multer = require('multer');
            let storage = multer.diskStorage({
                destination: '/public/my-uploads',
                  filename: function (req, file, cb) {
                    cb(null, file.fieldname   '-'   Date.now())
                  }
           });

  const upload = multer({dest:'storage/'}).single('file');

app.post('/upload', upload, (req , res) => {
      console.log(req.files) // this does log the uploaded image data.
    })

CodePudding user response:

Try this File Storage For Save image in Local

const fileStorage = multer.diskStorage({
      destination: (req, file, cb) => {
        cb(null, "images");
      },
      filename: (req, file, cb) => {
        cb(
          null,
          new Date().toISOString().replace(/:/g, "-")   "-"   file.originalname
        );
      },
    });

This happens because you are using windowsOS and where you learned it may be using macOS in windows Path have to setted as mentioned in the above code.

You simply set the file name as given in my answer and destination is root dir "./images"...

  • Related