1

I am newbie in backend especially nodejs, I am using nestjs as framework. I want to make the name of my uploaded file https://mydomain/files/example-image.png, but I get an error like this:

Error: ENOENT: no such file or directory,
open 'C:\***\***\***\nestjs-learning\files\http:\localhost:3001\files\9795f8f8147410d8fa07a4de5a92e0678.png'

and here is my code to name the file:

FileInterceptor('image', {
  storage: diskStorage({
    destination: './files',
    filename: (req, file, cb) => {
      const randomName = Array(32)
        .fill(null)
        .map(() => Math.round(Math.random() * 16).toString(16))
        .join('');
      return cb(null, `http://localhost:3001/files/${randomName}${extname(file.originalname)}`);
    },
  }),
}),

Is this way wrong? Is there a specific way to name the file? Please help me solve this.

Mostafa Fakhraei
  • 3,409
  • 3
  • 11
  • 25

1 Answers1

0

the problem is you add the url-path in file name try this

      FileInterceptor('image', {
      storage: diskStorage({
        destination: './files', // path whare save the files
        filename: (req, file, cb) => {
          const randomName = Array(32)
            .fill(null)
            .map(() => Math.round(Math.random() * 16).toString(16))
            .join('');
          return cb(null,`${randomName}${extname(file.originalname)}`); // return only file name
        },
      }),
    }),
Kareem Adel
  • 371
  • 2
  • 10