Sample code for 30+ languages & platforms
React Native

Save String Variable to File on FTP Server

See more FTP Examples

Save the contents of a string variable to a file on an FTP server.

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();

  ftp.hostname = 'ftp.example.com';
  ftp.username = 'login';
  ftp.password = 'password';

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

  // Change to the remote directory where the existing file is located.
  try {
    await ftp.changeRemoteDirAsync('junk');
  } catch {
    console.log(ftp.lastErrorText);
    return;
  }

  // Upload the contents of a string to a remote file.
  const fileContents = 'To be, or not to be: that is the question.';
  const remoteFilename = 'hamletQuote.txt';

  try {
    await ftp.putFileFromTextDataAsync(remoteFilename, fileContents, 'utf-8');
  } catch {
    console.log(ftp.lastErrorText);
    return;
  }

  await ftp.disconnectAsync();

  console.log('String Uploaded!');
}