Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

loAi = createobject("CkAi")

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

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

lcContentType = "image/jpeg"
lcLocalFilePath = "qa_data/jpg/starfish.jpg"
lcFilenameOnServer = "starfish.jpg"

// Upload directly from a file.
lcFile_id = loAi.UploadFile(lcLocalFilePath,lcContentType)
if (loAi.LastMethodSuccess = .F.) then
    ? loAi.LastErrorText
    ? "AI File Upload Failed."
else
    ? "File ID: " + lcFile_id
    ? "File uploaded."
endif

// Upload from the contents of a Chilkat BinData
loBd = createobject("CkBinData")
llSuccess = loBd.LoadFile(lcLocalFilePath)
if (llSuccess = .F.) then
    ? loBd.LastErrorText
    release loAi
    release loBd
    return
endif

lcFile_id = loAi.UploadFileBd(loBd,lcFilenameOnServer,lcContentType)
if (loAi.LastMethodSuccess = .F.) then
    ? loAi.LastErrorText
    ? "AI File Upload Failed."
else
    ? "File ID: " + lcFile_id
    ? "File uploaded."
endif



release loAi
release loBd