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

Upload an Image File to an AI Provider (OpenAI, Google, Antropic, X)

See more AI Examples

Uploads an image file to an AI provider using the provider's File API and returns the id that can later be used to reference the file in an query. This currently works with ChatGPT and Gemini, but not other AI's. Most AI's currently don't have the ability to reference pre-uploaded image data in conversations.

Chilkat React Native Downloads

React Native
import { Ai, BinData } from '@chilkat/react-native'

async function chilkatExample() {
  const ai = new Ai();

  // Can currently be "openai" or "google" because these are the only AI's that can reference pre-loaded image data by file ID.
  ai.provider = 'openai';
  // Use your provider's API key.
  ai.apiKey = 'MY_API_KEY';

  // We can upload directly from a file in the filesystem, or from a Chilkat BinData.

  const contentType = 'image/jpeg';
  const localFilePath = 'qa_data/jpg/starfish.jpg';
  const filenameOnServer = 'starfish.jpg';

  // Upload directly from a file.
  let fileId = await ai.uploadFileAsync(localFilePath, contentType);
  if (!ai.lastMethodSuccess) {
    console.log(ai.lastErrorText);
    console.log('AI File Upload Failed.');
  } else {
    console.log(`File ID: ${fileId}`);
    console.log('File uploaded.');
  }

  // Upload from the contents of a Chilkat BinData
  const bd = new BinData();
  try {
    bd.loadFile(localFilePath);
  } catch {
    console.log(bd.lastErrorText);
    return;
  }

  try {
    fileId = await ai.uploadFileBdAsync(bd, filenameOnServer, contentType);
    console.log(`File ID: ${fileId}`);
    console.log('File uploaded.');
  } catch {
    console.log(ai.lastErrorText);
    console.log('AI File Upload Failed.');
  }
}