ENOENT: no such file or directory .?

后端 未结 14 1520
梦谈多话
梦谈多话 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:41

    So the answer is in the tutorials comments section on youtube. Instead of:

    cb(null, new Date().toISOString() + file.originalname);
    

    do:

    cb(null, Date.now() + file.originalname);
    

    Simple as.

    0 讨论(0)
  • 2020-12-09 17:41

    create folder uploads near app.js file

    for this line

    app.use('/uploads', express.static('uploads'));
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-09 17:43

    in product.js:

    After new Date().toISOString() add replace() to change ":" to an accepted character.

    Windows OS doesn't accept files with a ":"

    The person on Youtube is using MAC OS

    E.g

    new Date().toISOString().replace(/:/g, '-')

    0 讨论(0)
  • 2020-12-09 17:43

    every thing is fine. problem is on this line cb(null, new Date().toISOString() + file.originalname); simply write cb(null,file.originalname); it will work. try to use in different way to add the date string with file name.

    0 讨论(0)
  • 2020-12-09 17:48

    I found this in the comments section, here: https://www.youtube.com/watch?v=srPXMt1Q0nY&list=PL55RiY5tL51q4D-B63KBnygU6opNPFk_q&index=10

    OK guys, in case someone has an issue with this in the file creation stage, that probably means you're working on Windows. Now, you don't need to feel discouraged and throw your computer to the trash (I actually like always having to find workarounds for my Windows :).

    There's at least a solution, and this is the one I found. My problem is the file does not get created because Windows does not accept filenames with colon (':') on it. My solution is rather simple. After I get the current date, I use replace() and a regexp to change that into a dash. Viola. It works!

    Just in case, this is one way to do it: filename: function(req, file, cb){ const now = new Date().toISOString(); const date = now.replace(/:/g, '-'); cb(null, date + file.originalname); }

    Hope it helps someone who´s working in windows.

    0 讨论(0)
提交回复
热议问题