Node A method returns a ReadStream type, how to save the read content to a file in the specified path

https://github.com/jyu213/ssh.
this get method that operates the FTP module can read the server file and return the ReadStream type. I need to download it to the local specified path. How to do the request?

Mar.17,2021

const fs = require('fs');
const readable = getReadableStreamSomehow();
const writable = fs.createWriteStream('file.txt');
// All the data from readable goes into 'file.txt'
readable.pipe(writable);

https://nodejs.org/api/stream.


sftp.getFile(fileName).then(data => {
// data ReadStream  data.pipe(require('fs').createWriteStream(`files/download/${fileName}.csv`));
  console.log(data)
}).catch(err => {
  console.log(err);
});

Note:

   const downLoadFile =  (blob, fileName)=> {
      if (window.navigator.msSaveOrOpenBlob) { // IE10
          navigator.msSaveBlob(blob, fileName);
      } else {
          let link = document.createElement('a');
          link.style.display = 'none';
  
          link.href = URL.createObjectURL(blob); //URL
          link.download = fileName;
          link.click(); // 
          URL.revokeObjectURL(link.href); //  URL.createObjectURL()  URL
      }
  }
    const downloadQrCode = () => {
      /*
      comments by wellen
      readableStream,~ 
      
      :
      1.:responseType: 'arryBuffer';
      2.arryBuffer:.then(response => response.arrayBuffer())
      3.blob
      */
      fetch(`${config.baseUrl}/api/v1/weChat/getwxacodeunlimit`, {
        method: 'POST',
        responseType: 'arryBuffer',
        headers: {
          'Content-Type': 'application/json;charset=UTF-8',
        },
        body: JSON.stringify({
          scene: id,
          is_hyaline: true,
          width: 500,
          page: 'pages/index/index',
        }),
      }).then(response => response.arrayBuffer())
        .then(body => {
          const blob = new Blob([body], { type: 'image/png' }); // excel
          const fileName = `${id}-${new Date().getTime()}.png`; // 
          downLoadFile(blob, fileName);
        });
    };
Menu