Sample code for 30+ languages & platforms
React Native

FTP Download File to a Stream

Demonstrates how to FTP download a file to a Chilkat stream.

Chilkat React Native Downloads

React Native
import { Ftp2, Stream } from '@chilkat/react-native'

async function chilkatExample() {
  // This example assumes the Chilkat FTP2 API to have been previously unlocked.
  // See FTP2 Unlock Sample for sample code.

  const ftp = new Ftp2();

  ftp.hostname = 'my-ftp-server.com';
  ftp.port = 21;
  ftp.username = 'mFtpLogin';
  ftp.password = 'myFtpPassword';
  ftp.authTls = true;
  ftp.passive = true;

  // Connect and login to the FTP server using TLS.
  try {
    await ftp.connectAsync();
  } catch {
    console.log(ftp.lastErrorText);
    return;
  }

  // Move to the sub-directory (from the FTP user's home directory) where the file is located.
  try {
    await ftp.changeRemoteDirAsync('temp');
  } catch {
    console.log(ftp.lastErrorText);
    return;
  }

  // Stream to this local file:
  const streamObj = new Stream();
  streamObj.sinkFile = 'c:/temp/qa_output/penguins2.jpg';

  try {
    await ftp.getFileToStreamAsync('penguins2.jpg', streamObj);
  } catch {
    console.log(ftp.lastErrorText);
    return;
  }

  await ftp.disconnectAsync();

  console.log('Success.');
}