javascript 无法在nestjs中上载图像

9wbgstp7  于 2023-01-24  发布在  Java
关注(0)|答案(1)|浏览(143)
hi 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)}`);
            },
          }),
        }),

这个方法是错误的吗?,有一个具体的方法来命名这文件?有人请帮助我解决这个问题

6fe3ivhb

6fe3ivhb1#

问题是你在文件名中添加url路径试试这个

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
        },
      }),
    }),

相关问题