Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loAi
LOCAL lcContentType
LOCAL lcLocalFilePath
LOCAL lcFilenameOnServer
LOCAL lcFile_id
LOCAL loBd

lnSuccess = 0

loAi = CreateObject('Chilkat.Ai')

* 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 = 0) 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('Chilkat.BinData')
lnSuccess = loBd.LoadFile(lcLocalFilePath)
IF (lnSuccess = 0) THEN
    ? loBd.LastErrorText
    RELEASE loAi
    RELEASE loBd
    CANCEL
ENDIF

lcFile_id = loAi.UploadFileBd(loBd,lcFilenameOnServer,lcContentType)
IF (loAi.LastMethodSuccess = 0) THEN
    ? loAi.LastErrorText
    ? "AI File Upload Failed."
ELSE
    ? "File ID: " + lcFile_id
    ? "File uploaded."
ENDIF

RELEASE loAi
RELEASE loBd