ENOENT: no such file or directory .?

后端 未结 14 1586
梦谈多话
梦谈多话 2020-12-09 17:15

This is error which am getting while post data and file. I have followed \'academind\' tutorial for building Restful API services, also i have been searching answer for this

14条回答
  •  长情又很酷
    2020-12-09 17:42

    I am doing the same course and I too had the same problem (i also use windows machine). The following worked for me:

    const hash = require('random-hash'); // you have to install this package:
    
    const fileStorage = multer.diskStorage({
        destination: (req, file, callback) => { //this is storing the file in the images folder
            callback(null, path.join(__dirname, '/Images'));
        },
    
        filename: (req, file, callback) => { //this is just setting a unique filename
            let temp = file.originalname.split('.');
            const filename = temp[0] + '-' + hash.generateHash({length: 5}) + '.' + temp[1]
            callback(null, filename);
        }
    });
    
    

    This creates a unique hash for the filenames as well

提交回复
热议问题