node.js FTP Package

Hi,

I have a request to place the PDF files generated by an app to be placed on a specified FTP site. I have been playing around with basic-ftp but am having issues due to the file not being on a local storage. Is there a way around this or should I be using a different FTP package?

1 Like

Hi @bruce-hayes

From the basic-ftp documentation, it will accept both a file path or a readable stream.

uploadFrom(readableStream | localPath, remotePath, [options]): Promise<FTPResponse>

Upload data from a readable stream or a local file to a remote file. If such a file already exists it will be overwritten.

So, you just need to convert the file you want to upload to a readable stream. Something like this should work

export async function run() {
    const ftp = require("basic-ftp")
    const { Readable } = require('stream');

    const client = new ftp.Client()
    const ftpConfig = {
        host: "",
        port: "",
        user: "",
        password: "",
        secure: true
    };

    const myBuffer = await myPDFAttachment.toBuffer();
    const stream = Readable.from(myBuffer);
    const remoteFtpPath = "my_pdf.pdf"

    // this includes better error logs
    client.ftp.verbose = true;
    console.log("About to connect");
    try {
        await client.access(ftpConfig)

        await client.uploadFrom(stream, remoteFtpPath)
    }
    catch (err) {
        console.log(err)
    }
    client.close()
}

@tielman Thanks for that, I have changed the uploading file to a readable Stream and it works now

1 Like