Sample code for 30+ languages & platforms
Visual FoxPro

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 the id that can later be used to reference the file in an query. This currently works with ChatGPT, Gemini, Claude, and Grok.

Chilkat Visual FoxPro Downloads

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

lnSuccess = 0

loAi = CreateObject('Chilkat.Ai')

* The provider can be "openai", "google", "claude", "grok", or any AI that supports uploading files for later reference by 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 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.
lcContentType = "application/json"
lcLocalFilePath = "qa_data/hamlet.json"
lcFilenameOnServer = "hamlet.json"

* 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 StringBuilder
loSb = CreateObject('Chilkat.StringBuilder')
lnSuccess = loSb.LoadFile(lcLocalFilePath,"utf-8")
IF (lnSuccess = 0) THEN
    ? loSb.LastErrorText
    RELEASE loAi
    RELEASE loSb
    CANCEL
ENDIF

lcFile_id = loAi.UploadFileSb(loSb,lcFilenameOnServer,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 BinData
loBd = CreateObject('Chilkat.BinData')
lnSuccess = loBd.LoadFile(lcLocalFilePath)
IF (lnSuccess = 0) THEN
    ? loBd.LastErrorText
    RELEASE loAi
    RELEASE loSb
    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 loSb
RELEASE loBd