【问题标题】:file download from ftp server in nodejs从nodejs中的ftp服务器下载文件
【发布时间】:2016-08-13 02:43:19
【问题描述】:

我有一个 Web 应用程序,其中单击按钮调用 REST API,该 API 获取 ftp 服务器中可用的文件列表并显示在 div 中,文件名上的超链接如下。

为此,我正在使用 jsftp 模块。

ftp.ls(".", function(err, res) {
  res.forEach(function(file) {
  console.log(file.name);
  //logic to display in div
});
});

我有另一个 REST API 用于从 FTP 服务器下载文件,该 API 在 div 中存在的文件名单击时调用,但它下载到 Web 应用程序部署的本地目录中,而不是使用以下代码在用户系统中下载,但是当用户单击时我想要该文件应在用户系统或计算机中下载的文件名。我怎样才能做到这一点。

ftp.get('remote/file.txt', 'local/file.txt', function(hadErr) {
if (hadErr)
  console.error('There was an error retrieving the file.');
else
  console.log('File copied successfully!');
}); 

【问题讨论】:

    标签: node.js file


    【解决方案1】:

    现在文件在您的计算机上,您将希望在客户端单击链接时将文件实际提供给客户端。使用 res.write 类似:res.write(file, 'binary');

    以下是一些有用的概念:Node.js send file to client

    【讨论】:

    • 谢谢,您的建议解决了我的问题。我只是在使用 res.download(path);
    最近更新 更多