0

I want to upload a file to sftp remote server using ssh2-sftp-client. I am taking the file from user in a post request along with destination. I am using multer to process the file.

const Client = require('ssh2-sftp-client');
const sftp = new Client();
const Multer = require("multer")
const multer = Multer({
    storage: Multer.MemoryStorage
});

app.put("/sftp", multer.single('file'), (req, res) => {
    sftpCredentials = req.query;
    sftp.connect({
        host: sftpCredentials.host,
        port: sftpCredentials.port,
        username: sftpCredentials.username,
        password: sftpCredentials.password
    }).then(res =>{
      sftp.put(req.file,req.query.destination);
    })
})

I am getting error :

TypeError: "string" must be a string, Buffer, or ArrayBuffer
Komal Bansal
  • 789
  • 2
  • 7
  • 20

1 Answers1

0
sftp.put(localfilepath, remoteFilepath)

for localfilepath use:

req.file.path

You have used "req.file" only. If you want to get the filename too, use: req.file.originalname

Second, make sure "req.query.destination" is giving you the destination path where you want to put file.

And, do use of logging. It makes life easier.

Gorav Singal
  • 508
  • 3
  • 11
  • req.file is an object which contains = `{ buffer:Buffer(85967) [137, 80, 78, …] encoding:"7bit" fieldname:"file" mimetype:"image/png" originalname:"sample.png" size:85967 }` – Komal Bansal Dec 20 '18 at 07:13
  • Well, I put answer from one of my code where I used multer. And, if you see official documentation of this module(https://www.npmjs.com/package/multer), See API section. You can see that "path" is there which denotes local filepath. – Gorav Singal Dec 20 '18 at 09:03