Sample code for 30+ languages & platforms
React Native

FTP Download with Progress Event Callbacks

See more FTP Examples

FTP download with progress event callbacks.

Chilkat React Native Downloads

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

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

  const ftp = new Ftp2();

  // Called with the percent completed (0..100) while an ...Async method runs.
  ftp.onPercentDone = (pctDone) => {
    console.log(`Percent Done: ${pctDone}`);
  };
  ftp.onProgressInfo = (name, value) => {
    console.log(`${name}: ${value}`);
  };

  ftp.hostname = 'ftp.someFtpServer.com';
  ftp.username = 'myLogin';
  ftp.password = 'myPassword';

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

  try {
    await ftp.loginAfterConnectOnlyAsync();
  } catch {
    console.log(ftp.lastErrorText);
    return;
  }

  // Change to the remote directory where the file is located.
  // This step is only necessary if the file is not in the home directory
  // of the FTP account.
  try {
    await ftp.changeRemoteDirAsync('junk');
  } catch {
    console.log(ftp.lastErrorText);
    return;
  }

  const localFilename = 'c:/temp/hamlet.xml';
  const remoteFilename = 'hamlet.xml';

  // Ensure that we get PercentDone callbacks.
  ftp.autoGetSizeForProgress = true;

  // Download the file.
  try {
    await ftp.getFileAsync(remoteFilename, localFilename);
  } catch {
    console.log(ftp.lastErrorText);
    return;
  }

  await ftp.disconnectAsync();

  console.log('File Downloaded!');
}