Sample code for 30+ languages & platforms
Dart 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 Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  final ai = CkAi();

  // 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.

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

  // Upload directly from a file.
  var fileId = ai.uploadFile(localFilePath, contentType);
  if (!ai.lastMethodSuccess) {
    print(ai.lastErrorText);
    print('AI File Upload Failed.');
  } else {
    print('File ID: $fileId');
    print('File uploaded.');
  }

  // Upload from the contents of a Chilkat BinData
  final bd = CkBinData();
  try {
    bd.loadFile(localFilePath);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  try {
    fileId = ai.uploadFileBd(bd, filenameOnServer, contentType);
    print('File ID: $fileId');
    print('File uploaded.');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    print('AI File Upload Failed.');
  }
}