Classic ASP
Classic ASP
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 theid that can later be used to reference the file in an query. This currently works with ChatGPT, Gemini, Claude, and Grok.
Chilkat Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0
set ai = Server.CreateObject("Chilkat.Ai")
' The provider can be "openai", "google", "claude", "grok", or any AI that supports uploading files for later reference by 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 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.
contentType = "application/json"
localFilePath = "qa_data/hamlet.json"
filenameOnServer = "hamlet.json"
' Upload directly from a file.
file_id = ai.UploadFile(localFilePath,contentType)
If (ai.LastMethodSuccess = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( ai.LastErrorText) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "AI File Upload Failed.") & "</pre>"
Else
Response.Write "<pre>" & Server.HTMLEncode( "File ID: " & file_id) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "File uploaded.") & "</pre>"
End If
' Upload from the contents of a StringBuilder
set sb = Server.CreateObject("Chilkat.StringBuilder")
success = sb.LoadFile(localFilePath,"utf-8")
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( sb.LastErrorText) & "</pre>"
Response.End
End If
file_id = ai.UploadFileSb(sb,filenameOnServer,contentType)
If (ai.LastMethodSuccess = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( ai.LastErrorText) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "AI File Upload Failed.") & "</pre>"
Else
Response.Write "<pre>" & Server.HTMLEncode( "File ID: " & file_id) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "File uploaded.") & "</pre>"
End If
' Upload from the contents of a BinData
set bd = Server.CreateObject("Chilkat.BinData")
success = bd.LoadFile(localFilePath)
If (success = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( bd.LastErrorText) & "</pre>"
Response.End
End If
file_id = ai.UploadFileBd(bd,filenameOnServer,contentType)
If (ai.LastMethodSuccess = 0) Then
Response.Write "<pre>" & Server.HTMLEncode( ai.LastErrorText) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "AI File Upload Failed.") & "</pre>"
Else
Response.Write "<pre>" & Server.HTMLEncode( "File ID: " & file_id) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "File uploaded.") & "</pre>"
End If
%>
</body>
</html>