JavaScript
JavaScript
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 theid 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.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat
v11.4.0 or greater.
var success = false;
var ai = new 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.
var contentType = "image/jpeg";
var localFilePath = "qa_data/jpg/starfish.jpg";
var filenameOnServer = "starfish.jpg";
// Upload directly from a file.
var file_id = ai.UploadFile(localFilePath,contentType);
if (ai.LastMethodSuccess == false) {
console.log(ai.LastErrorText);
console.log("AI File Upload Failed.");
}
else {
console.log("File ID: " + file_id);
console.log("File uploaded.");
}
// Upload from the contents of a Chilkat BinData
var bd = new CkBinData();
success = bd.LoadFile(localFilePath);
if (success == false) {
console.log(bd.LastErrorText);
return;
}
file_id = ai.UploadFileBd(bd,filenameOnServer,contentType);
if (ai.LastMethodSuccess == false) {
console.log(ai.LastErrorText);
console.log("AI File Upload Failed.");
}
else {
console.log("File ID: " + file_id);
console.log("File uploaded.");
}