5

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

1565986223
  • 6,420
  • 2
  • 20
  • 33
cool
  • 327
  • 1
  • 6
  • 14

6 Answers6

7

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))
    }
});
cool
  • 327
  • 1
  • 6
  • 14
0

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
Akber Iqbal
  • 14,487
  • 12
  • 48
  • 70
Banny
  • 111
  • 1
  • 6
0

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.

0

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',
0
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

Emre Kilinc Arslan
  • 2,019
  • 2
  • 16
  • 12
-1
  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);
        }
    });
Amit Kumar
  • 11
  • 2
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Adrian Mole Sep 05 '20 at 00:18