![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(DataFlex) Upload a File to an AI Provider (OpenAI, Google, Antropic, X)See more AI ExamplesUploads 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.Note: This example requires Chilkat v11.4.0 or greater.
Use ChilkatAx-win32.pkg Procedure Test Boolean iSuccess Handle hoAi String sContentType String sLocalFilePath String sFilenameOnServer String sFile_id Variant vSb Handle hoSb Variant vBd Handle hoBd String sTemp1 Boolean bTemp1 Move False To iSuccess Get Create (RefClass(cComChilkatAi)) To hoAi If (Not(IsComObjectCreated(hoAi))) Begin Send CreateComObject of hoAi End // The provider can be "openai", "google", "claude", "grok", or any AI that supports uploading files for later reference by ID. Set ComProvider Of hoAi To "openai" // Use your provider's API key. Set ComApiKey Of hoAi To "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. Move "application/json" To sContentType Move "qa_data/hamlet.json" To sLocalFilePath Move "hamlet.json" To sFilenameOnServer // Upload directly from a file. Get ComUploadFile Of hoAi sLocalFilePath sContentType To sFile_id Get ComLastMethodSuccess Of hoAi To bTemp1 If (bTemp1 = False) Begin Get ComLastErrorText Of hoAi To sTemp1 Showln sTemp1 Showln "AI File Upload Failed." End Else Begin Showln "File ID: " sFile_id Showln "File uploaded." End // Upload from the contents of a StringBuilder Get Create (RefClass(cComChilkatStringBuilder)) To hoSb If (Not(IsComObjectCreated(hoSb))) Begin Send CreateComObject of hoSb End Get ComLoadFile Of hoSb sLocalFilePath "utf-8" To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoSb To sTemp1 Showln sTemp1 Procedure_Return End Get pvComObject of hoSb to vSb Get ComUploadFileSb Of hoAi vSb sFilenameOnServer sContentType To sFile_id Get ComLastMethodSuccess Of hoAi To bTemp1 If (bTemp1 = False) Begin Get ComLastErrorText Of hoAi To sTemp1 Showln sTemp1 Showln "AI File Upload Failed." End Else Begin Showln "File ID: " sFile_id Showln "File uploaded." End // Upload from the contents of a BinData Get Create (RefClass(cComChilkatBinData)) To hoBd If (Not(IsComObjectCreated(hoBd))) Begin Send CreateComObject of hoBd End Get ComLoadFile Of hoBd sLocalFilePath To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoBd To sTemp1 Showln sTemp1 Procedure_Return End Get pvComObject of hoBd to vBd Get ComUploadFileBd Of hoAi vBd sFilenameOnServer sContentType To sFile_id Get ComLastMethodSuccess Of hoAi To bTemp1 If (bTemp1 = False) Begin Get ComLastErrorText Of hoAi To sTemp1 Showln sTemp1 Showln "AI File Upload Failed." End Else Begin Showln "File ID: " sFile_id Showln "File uploaded." End End_Procedure |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.