Sample code for 30+ languages & platforms
React Native Requires Chilkat v11.0.0+

Co:Z SFTP Binary File Download (from z/OS IBM Mainframe)

See more SFTP Examples

Demonstrates how to download a binary file, such as a .zip, from a Co:Z SFTP server on a z/OS IBM Mainframe.

Chilkat React Native Downloads

React Native
import { SFtp, SFtpDir } 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 sftp = new SFtp();

  // Connect to the SSH server.  
  const hostname = 'sftp.example.com';
  const port = 22;
  try {
    await sftp.connectAsync(hostname, port);
  } catch {
    console.log(sftp.lastErrorText);
    return;
  }

  try {
    await sftp.authenticatePwAsync('myLogin', 'myPassword');
  } catch {
    console.log(sftp.lastErrorText);
    return;
  }

  try {
    await sftp.initializeSftpAsync();
  } catch {
    console.log(sftp.lastErrorText);
    return;
  }

  // To download a binary file from the Co:Z SFTP server, 
  // we must switch to binary mode in the following unconventional way.
  // We pretend to fetch a directory listing for "/+mode=binary"
  // This has the effect of putting the server in binary mode for transfers.
  let handle: string;
  try {
    handle = await sftp.openDirAsync('/+mode=binary');
  } catch {
    console.log(sftp.lastErrorText);
    return;
  }

  // Download the "directory listing" (but it's not actually a directory listing, and we'll just discard it.)

  const dirListing = new SFtpDir();
  try {
    await sftp.readDirListingAsync(handle, dirListing);
  } catch {
    console.log(sftp.lastErrorText);
    return;
  }

  // Close the directory handle:
  try {
    await sftp.closeHandleAsync(handle);
  } catch {
    console.log(sftp.lastErrorText);
    return;
  }

  // Download the binary file:
  const localFilePath = 'c:/temp/test.zip';
  const remoteFilePath = 'test.zip';
  try {
    await sftp.downloadFileByNameAsync(remoteFilePath, localFilePath);
  } catch {
    console.log(sftp.lastErrorText);
    return;
  }

  console.log('Success.');
}