Reputation: 385
When I'm trying to upload images and save in public/upload_files folder through postman it shows this error
node -v v10.15.3
npm -v 6.9.0
"Error: ENOENT: no such file or directory"
This is my code
const express = require('express');
const router = express.Router();
const multer = require('multer');
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null,'./public/uploaded_files');
},
filename: function(req, file, cb) {
cb(null,new Date().toISOString() + file.originalname);
}
});
const upload = multer({storage:storage});
router.post('/', upload.single('file'), (req,res,next) => {
console.log(req.file);
});
module.exports = router;
I'm just trying to save the images in the following folder public/upload_files
Upvotes: 5
Views: 9315
Reputation: 2189
new Date().toISOString()
is not a properName for windows file system to save a file. try using
Date.now()
or
new Date().getTime()
or anything which doesnt include invalid characters
Upvotes: 0
Reputation: 1
Works with me...
cb(null, './uploads/');
and...
cb(null, new Date().getTime() + file.originalname);
the final result is...
filename: '1656962448485IMG-20200414-WA0030.jpg',
path: 'uploads\\1656962448485IMG-20200414-WA0030.jpg',
Upvotes: 0
Reputation: 385
I made few changes to my code and it worked.
I added this line
cb(null,path.join(__dirname,'../upload'))
and this
cb(null,Date.now() + path.extname(file.originalname))
code
var storage = multer.diskStorage({
destination: function(req, file, cb)
{
cb(null,path.join(__dirname,'../upload'))
},
filename: function(req, file, cb)
{
cb(null,Date.now() + path.extname(file.originalname))
}
});
Upvotes: 7
Reputation: 11
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, './public/uploaded_files');
},
filename: function(req, file, cb) {
// cb(null, new Date().toISOString() + file.originalname) // this is wrong
cb(null, Date.now() + file.originalname);
}
});
Upvotes: -1
Reputation: 1
It may due to a forbidden filename or OS action. Did you ran the program in different OS? For eg: Some OS does not allow filename with some special characters as produced by the new Date().toISOString() function. Extra: I think this code is from the Node js course from Max.
Upvotes: 0
Reputation: 111
Use
cb(null, Date.now() + file.originalname);
instead of
cb(null, new Date().toISOString() + file.originalname);
to prevent
"error": "ENOENT: no such file or directory
Upvotes: 0