ENOENT: no such file or directory .?

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

    You don't have permission to access /uploads/ on this server.

    Try the following:

    sudo chmod -R 777 /uploads
    
    0 讨论(0)
  • 2020-12-09 17:29

    Note: In order to remove all special character, we can use replace function as

    const cleanVariable = mixSpecialCharters.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');
    
    0 讨论(0)
  • 2020-12-09 17:32

    I had a similar error and this is how I resolved it. After using the replace method, I changed './uploads/images/' to 'uploads/images'. In this case, multer created the folder automatically. So you have something like this

    const storage = multer.diskStorage({
        destination: function(req, file, cb) {
        cb(null, 'uploads/');
        },
        filename: function(req, file, cb) {
         cb(null, new Date().toISOString().replace(/:/g, '-')+ file.originalname);
          }
      });
    

    For Windows users.

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

    I think if you work with Windows OS, you should use another methods of Date().i write code like this:

    filename:(req,file,cb)=>{ cb(null,new Date().toDateString()+file.originalname) }
    
    0 讨论(0)
  • 2020-12-09 17:37

    Try the following:

    1. Require this as a constant (const path = require('path');)
    2. Change this line

      cb(null, './uploads/');

    With this:

    cb(null, path.join(__dirname, '/uploads/'));
    

    As I can see, you are trying to get a path that is not on served on the server, but rather a path that is on the server machine.

    UPDATE

    Try also changing this

    app.use('/uploads', express.static('uploads'));
    

    To this:

    app.use(express.static(__dirname));
    

    In order to expose the __dirname for static files.

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

    You should change the file name. Because ':' is not allowed in Windows.

    Eg:

    const storage = multer.diskStorage({
        destination: function(req, file, cb){
            cb(null,'./uploads/');
        },
        filename: function(req,file,cb){
            cb(null, new Date().toISOString().replace(/:/g, '-') +'-'+ file.originalname);
        }
    });
    
    0 讨论(0)
提交回复
热议问题