upload files to remote server using multer sftp in express Node js?

前端 未结 1 1393
刺人心
刺人心 2021-01-17 17:37

I\'m trying to upload the files to remote server using multer-sftp in node js. since i\'m following the official docs npm multer-sftp. Previously i\'ve uploading the files t

1条回答
  •  花落未央
    2021-01-17 18:07

    For Your Error, there are two possibilities

    1. port no 22 is not open state, also not able to access that folder
    2. Check your folder directory in domain

    Uploading Files to remote server using multer-sftp is easy and flexible way. also we can upload the files to remote server with scp, ssh techniques in node js.

    Working Code:

    exports.newFileUpload =  function(req , res , next){     
        var storage = sftpStorage({
          sftp: {
            host: 'hostname',
            port: 22,
            username: 'username',
            password: 'password'
    
          },
          destination: function (req, file, cb) {
            cb(null, 'images/')
          },
          filename: function (req, file, cb) {
            cb(null, file.fieldname + '-' + Date.now())
          }
        })
    
        var upload = multer({ storage: storage }).array('file');
    
        upload(req,res,function(err){
            logger.debug(JSON.stringify(req.body));
                  logger.debug(JSON.stringify(req.files));
              if(err){
                   logger.debug("Error Occured", JSON.stringify(err));
                   res.json({error_code:1,err_desc:err});
              } else{
                   logger.debug("Files uploaded successfully");
                  res.json({error_code:0,err_desc:null});
              }
          });
    }
    

    Note: When using 'multer-sftp' port no 22 is open in remote server.

    Hope it helps !

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