0
votes

Multer is not uploading files on server directory below my source code to upload file. file object is created that means when i log req.files file are present but not uploading to directory.im using POSTMAN for API Testing sending file through formdata->uploadfiles and with key "uploadfiles" destinition function is not called because log inside function is not console the name "Hello "

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        console.log("Hello Umesh");
        cb(null, '../resources/uploads'); // Absolute path. Folder must exist, will not be created for you.
      },
      filename: function (req, file, cb) {
        console.log("Hello Umesh");
        cb(null, file.fieldname + '-' + Date.now());
      }
});

const upload = multer({
                         storage: storage,
                         limit:{filesize:10}

                    }).single('uploadfiles');

router.post('/save', function (req, res) { 
    upload(req, res, function (err) {
        if (err) {
            res.json({
                success: false,
                message:err
            });
        }
        else{
        res.json({
            success: true,
            message: req.file
        });
    }
    })
});

source code files are inside routes this is log result when req.files

1
are you sure the directory at ../resources/uploads exists? (Maybe you want to use __dirname instead of ../ to make sure it's the correct pathdjaax
added image where folder exist.my code files inside routesUmesh Shende
does a folder uploads exist inside the resources folder?djaax
yes but i wonder console is not printing the message inside destinition folder why?Umesh Shende

1 Answers

0
votes

Try to use the absolute path to the storage location:

cb(null, require('path').join(__dirname, '..', 'resources', 'uploads')); 

instead:

cb(null, '../resources/uploads');

Update:

Since the working directory for functions from the fs module is the project's root folder (if you run the project with the npm start command), you can simply use this:

cb(null, 'resources/uploads');