使用multer-ftp

时间:2018-01-05 18:22:15

标签: javascript node.js ftp multer

我正在尝试使用multer-ftp将文件上传到ftp。它成功上传到ftp然而我需要更改文件的名称。有没有办法做到这一点?

var upload = multer({
  storage: new FTPStorage({
    basepath: '/path',
    ftp: {
      host: host,  
      secure: false,
      user: user,  
      password: pwd 
    }
  })
}).single('fileupload');

app.post('/getfiles', function (req, res, next) {
  upload(req,res, function(err){
    if(err){  
      res.send('Error uploading file - ' + err);
    }else{ 
       res.send('File is uploaded - ' + JSON.stringify(req.file)); 
    }
  })    
})

在req.file中,它具有上传时的原始文件名。我怎样才能使用multer-ftp上传文件而不是它出来的名称(示例格式为5acfbabc8430fb3d311ae365f448.png

1 个答案:

答案 0 :(得分:2)

选中此code,使用destination选项重命名文件。

var upload = multer({
  storage: new FTPStorage({
    basepath: '/path',
    destination: function (req, file, options, callback) {
       callback(null, path.join(options.basepath, file.originalname))
    },
    ftp: {
      host: host,  
      secure: false,
      user: user,  
      password: pwd 
    }
  })
}).single('fileupload');

//更正后的代码