2017-05-06 50 views
0

使用此代碼我嘗試遠程服務器上發送文件:如何將文件上傳到遠程服務器上的特定文件夾multer-ftp?

var upload = multer({ 
    storage: new ftpStorage({ 
     basepath: '/uploads/', 

     ftp: { 
      host: 'samplehost.com', 
      secure: false, // enables FTPS/FTP with TLS 
      user: 'login', 
      password: 'password' 
     } 
    }) 
}); 

卻始終保存文件的根目錄,不上傳,我該如何解決這一問題?

回答

0

代替multer-ftp,使用multer-sftp將文件上傳到遠程服務器是一種簡單而靈活的方式。我們也可以通過節點js中的scp,ssh技術將文件上傳到遠程服務器。

工作代碼:

exports.newFileUpload = function(req , res , next){ 

    // sftp settings  
    var storage = sftpStorage({ 
     sftp: { 
     host: 'hostname', 
     port: 22, 
     username: 'username', 
     password: 'password' 

     }, 
     destination: function (req, file, cb) { 
     cb(null, 'images/') // designation folder in host 
     }, 
     filename: function (req, file, cb) { 
     // file name settings 
     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}); 
      } 
     }); 
} 

注意:當使用 'multer-SFTP' 端口號22是在遠程服務器上打開。

官方文檔multer-sftp

希望它能幫助!

+0

雖然到外部資源的鏈接可能很有價值,但您需要在SO上直接在您的帖子中解釋和包含答案的本質。請這樣做。 –

+0

@MaciejJureczko。感謝您的建議,我已經更新了答案 – Jackie

相關問題