React Native Requires Chilkat v11.4.0+
React Native
Upload a File to an AI Provider (OpenAI, Google, Antropic, X)
See more AI Examples
Uploads a file to an AI provider using the provider's File API and returns theid that can later be used to reference the file in an query. This currently works with ChatGPT, Gemini, Claude, and Grok.
Chilkat React Native Downloads
import { Ai, BinData, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
const ai = new Ai();
// The provider can be "openai", "google", "claude", "grok", or any AI that supports uploading files for later reference by 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 StringBuilder, or from a Chilkat BinData.
// Some AI providers require a content-type.
// Also, some AI providers are picky about what content-type's are accepted.
// Check the AI provider's documentation.
// "application/json" is generally always acceptable.
// "text/plain" can be used as a fallback for any text file.
const contentType = 'application/json';
const localFilePath = 'qa_data/hamlet.json';
const filenameOnServer = 'hamlet.json';
// 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 StringBuilder
const sb = new StringBuilder();
try {
sb.loadFile(localFilePath, 'utf-8');
} catch {
console.log(sb.lastErrorText);
return;
}
try {
fileId = await ai.uploadFileSbAsync(sb, filenameOnServer, contentType);
console.log(`File ID: ${fileId}`);
console.log('File uploaded.');
} catch {
console.log(ai.lastErrorText);
console.log('AI File Upload Failed.');
}
// Upload from the contents of a 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.');
}
}