ENOENT: no such file or directory .?

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

    This what worked for me. I changed './uploads/' into '__dirname' so that it can find the correct directory/filename anywhere on your computer.

    const storage = multer.diskStorage({
      destination: function(req, file, cb) {
        cb(null, __dirname);
      },
      filename: function(req, file, cb) {
        cb(null, new Date().toISOString() + file.originalname);
      }
    });
    

    Because when you set a specific folder name/directory you limit your image directory to be only or should be in that folder.

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

    I came across the same error while saving the file. The path I provided in callback didn't exist already that's why I got that error

    const fs = require('fs');
    const storage = multer.diskStorage({
      destination: function(req, file, cb) {
        fs.mkdir('./uploads/',(err)=>{
           cb(null, './uploads/');
        });
      },
      filename: function(req, file, cb) {
        cb(null, new Date().toISOString() + file.originalname);
      }
    });
    

    With filesystem I created the same folder, In case of folder exists the err gets value but here nothing to worry about as we have that folder. This worked for me. hope this would help

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