N
N
nurzhannogerbek2019-01-26 23:09:54
JavaScript
nurzhannogerbek, 2019-01-26 23:09:54

Should I use res.sendFile in Node.js to send a csv file as a response to the user?

Hello comrades! Please help me figure it out.
When accessing a specific url address, you need to download a file from a remote SFTP server and return it to the user in the browser.
Used the ssh2-sftp-client library for this task. The file is downloaded from the remote server to the user's local machine via the fastGet method . Further, this file must be sent as a response to the user. Used the following code, but it turns out to create another exactly the same file in the user's local directory. How to fix it?

router.get('/', (req, res) => {
  const remotePath = '/reports/' + 'daly.csv'

  const localePath = path.join(process.env.HOME || process.env.USERPROFILE, 'downloads/daly.csv')

  sftp.connect(config.sftpServer, 'on').then(() => {
    const options = {
      root: path.join(process.env.HOME || process.env.USERPROFILE, 'downloads'),
      dotfiles: 'deny',
      headers: {
        'x-timestamp': Date.now(),
        'x-sent': true
      }
    }

    sftp.fastGet(remotePath, localePath, {}).then(() => {
      res.setHeader('Content-disposition', 'attachment; filename=daily.csv')
      res.sendFile('daly.csv', options)
    })

  }).catch((error) => {
    console.log(error)
  })
})

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question