0

我正在尝试从 ftp 加载多个文件,但出现错误:“结束后写入”。我无法找到问题。

连接.js

async function getFtpFileContent(_path) {
  const ftpConfig = {
    host: config.FTPhost.replace('https://', ''),
    username: config.FTPusername,
    password: config.FTPpassword
  };
  let ftpFileContent;
  let sftp = new StfpClient();
  try {
    await sftp.connect(ftpConfig);
    const stream = await sftp.get(_path);
    ftpFileContent = await readStream(sftp, stream);
    stream.close();
    await sftp.end();
  } catch (error) {
    console.log('FTP Error: \n', error);
  }

  return ftpFileContent;
}


function readStream(sftp, stream) {
  return new Promise((resolve, reject) => {
    let body;
    stream.on('data', chunk => {
      body += chunk;
    });
    stream.on('end', () => {
      resolve(body);
    });
    stream.on('error', err => reject(err));
  });
}

调用下一个方法:

async function getFiles(req, res) {
  // first file
  let fileContent = await conn.getFtpFileContent(filePath);

  // second file
  let fileContent2 = await conn.getFtpFileContent(filePath2);
}

当我第二次调用 getFtpFileContent 方法时,我得到了提到的错误(“结束后写入”)。

请你帮助我好吗?我在哪里做错了?

4

2 回答 2

0

我不确定你的问题出在哪里,但你应该在 getFtpFileContent 函数中调用 stream.close() 吗?我没有看到有关流的关闭方法的任何信息,只有关闭事件。

于 2019-01-28T02:09:58.407 回答
0

我发现了这个问题。问题出在我后来使用的 CSV 转换器中。转换器使用了转换后未关闭的可写流。

于 2019-01-28T12:26:18.127 回答