Skip to main content

Connect to an FTP server

Establish a connection to your FTP server using the FTPClient class. You must call connect() before performing any file operations.
import { FTPClient } from "workerd-ftp";

const ftp = new FTPClient('ftp.example.com', {
  port: 21,
  user: 'username',
  pass: 'password',
  secure: false
});

await ftp.connect();
All connection options are optional except the hostname. The default port is 21, and default credentials are ‘anonymous’.

Upload a file

Upload content to the FTP server using the upload() method. You need to convert your data to Uint8Array format.
import { FTPClient } from "workerd-ftp";

const ftp = new FTPClient('ftp.example.com', {
  user: 'username',
  pass: 'password'
});

await ftp.connect();

// Upload text content
const content = 'Hello, World!';
const data = new TextEncoder().encode(content);
await ftp.upload('hello.txt', data);

console.log('File uploaded successfully!');
The upload() method handles the entire upload process, including opening the data connection and finalizing the transfer.

Download a file

Download files from the FTP server using the download() method. The method returns a Uint8Array that you can decode as needed.
import { FTPClient } from "workerd-ftp";

const ftp = new FTPClient('ftp.example.com', {
  user: 'username',
  pass: 'password'
});

await ftp.connect();

// Download file
const fileData = await ftp.download('hello.txt');
const text = new TextDecoder().decode(fileData);

console.log('File content:', text);
Use directory navigation methods to move around the FTP server’s file system.
// Get the current working directory
const currentDir = await ftp.cwd();
console.log('Current directory:', currentDir);

Disconnect from the server

Always close the connection when you’re done to avoid leaving loose connections.
import { FTPClient } from "workerd-ftp";

const ftp = new FTPClient('ftp.example.com', {
  user: 'username',
  pass: 'password'
});

try {
  await ftp.connect();
  
  // Perform operations
  await ftp.upload('test.txt', new TextEncoder().encode('test'));
  
} finally {
  // Always close the connection
  await ftp.close();
}
Always call close() when you’re done with the FTP connection. Use a try/finally block to ensure cleanup even if errors occur.

Complete example

Here’s a complete example that connects, uploads a file, downloads it back, and disconnects:
import { FTPClient } from "workerd-ftp";

async function ftpExample() {
  const ftp = new FTPClient('ftp.example.com', {
    port: 21,
    user: 'username',
    pass: 'password',
    secure: false
  });

  try {
    // Connect to server
    await ftp.connect();
    console.log('Connected to FTP server');

    // Get current directory
    const cwd = await ftp.cwd();
    console.log('Working directory:', cwd);

    // Upload a file
    const content = 'Hello from workerd-ftp!';
    await ftp.upload('test.txt', new TextEncoder().encode(content));
    console.log('File uploaded');

    // Download the file back
    const downloaded = await ftp.download('test.txt');
    const text = new TextDecoder().decode(downloaded);
    console.log('Downloaded content:', text);

    // List files in current directory
    const files = await ftp.list();
    console.log('Files in directory:', files);

  } catch (error) {
    console.error('FTP error:', error);
  } finally {
    // Clean up connection
    await ftp.close();
    console.log('Disconnected');
  }
}

ftpExample();

Build docs developers (and LLMs) love