代替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
希望它能幫助!
雖然到外部資源的鏈接可能很有價值,但您需要在SO上直接在您的帖子中解釋和包含答案的本質。請這樣做。 –
@MaciejJureczko。感謝您的建議,我已經更新了答案 – Jackie