Upload Lua Reference Documentation
chilkat.Upload
Current Version: 11.7.0
Markdown version of this page for AI tools (v 11.7.0) · all AI docs
Chilkat.Upload
Add one or more local file references and send them to a server endpoint
using HTTP POST.
Add ordinary form fields alongside file parts when the server expects
metadata, tokens, IDs, or other POST parameters.
Include application-specific HTTP headers for authorization schemes,
API requirements, content negotiation, or server-side routing.
Use a simple blocking upload, begin an upload in the background, or use
async task-based workflows where supported.
Monitor bytes sent, percent uploaded, current filename, upload state,
and optionally throttle upload bandwidth.
Configure Basic authentication, HTTP/SOCKS proxy settings, HTTPS/TLS
behavior, cipher restrictions, protocol versions, and certificate pinning.
For an extended overview, see
Upload Class Overview.
Upload files with HTTP POST, form fields, headers, progress tracking, and TLS control.
Chilkat.Upload sends files to a web server using HTTP POST,
typically as a multipart/form-data upload. It supports one or more file
references, custom form parameters, custom HTTP headers, blocking and
asynchronous uploads, progress monitoring, upload throttling, Basic HTTP
authentication, proxy settings, HTTPS/TLS configuration, timeout control, and
inspection of the HTTP response returned by the server.
File upload requests
Form parameters
Custom headers
Blocking or async upload
Progress and throttling
Authentication, proxy, and TLS
Hostname, Port, Ssl,
and upload Path, add file references with
AddFileReference, add any form parameters or headers, then call
BlockingUpload or start an asynchronous upload. After completion,
inspect ResponseStatus, ResponseHeader,
ResponseBody, and LastErrorText to determine what
the server returned.
Object Creation
local chilkat = require("chilkat")
local upload = chilkat.Upload.new()Creates the object (chilkat.Upload() does the same). Lua's garbage collector frees it when it is no longer referenced; upload:dispose() frees it at once, and in Lua 5.4 and later local upload <close> = chilkat.Upload.new() frees it when the block ends. Calling a method of a disposed object raises an error.
Errors
A method that fails returns nil followed by the error text, as io.open does: local body, err = upload:method(...), where err is the object's last_error_text. Wrap the call in assert() to raise an error with that text instead. Methods that answer a question return true or false, numbers are returned as they are (check last_method_success where the description says a value means failure), and property reads and writes never fail for Chilkat reasons.
Misuse raises a Lua error: a wrong argument type or a missing argument (bad argument #1 to ...), an unknown property name, a write to a read-only property, an out-of-range integer, or an object of the wrong class.
Names are Chilkat's in snake_case: QuickGetStr is quick_get_str, LastErrorText is last_error_text. Properties are fields (upload.verbose_logging = true) and methods use the colon (upload:method(...)). Strings are UTF-8; binary data is a Lua string of bytes. Indexes are 0-based, as in every Chilkat API. Unlocking: call chilkat.unlock_bundle("...") once at startup.
Properties
AbortCurrent
local boolVal = upload.abort_current
upload.abort_current = boolVal
When set to true, causes the currently running method to abort. Methods that always finish quickly (i.e.have no length file operations or network communications) are not affected. If no method is running, then this property is automatically reset to false when the next method is called. When the abort occurs, this property is reset to false. Both synchronous and asynchronous method calls can be aborted. (A synchronous method call could be aborted by setting this property from a separate thread.)
BandwidthThrottleUp
local intVal = upload.bandwidth_throttle_up
upload.bandwidth_throttle_up = intVal
If non-zero, limits (throttles) the upload bandwidth to approximately this maximum number of bytes per second. The default value of this property is 0.
ChunkSize
local intVal = upload.chunk_size
upload.chunk_size = intVal
The chunk size (in bytes) used by the underlying TCP/IP sockets for uploading files. The default value is 65535.
topClientIpAddress
local strVal = upload.client_ip_address
upload.client_ip_address = strVal
The IP address to use for computers with multiple network interfaces or IP addresses. For computers with a single network interface (i.e. most computers), this property should not be set. For multihoming computers, the default IP address is automatically used if this property is not set.
The IP address is a string such as in dotted notation using numbers, not domain names, such as 165.164.55.124.
CurrentFilename
local strVal = upload.current_filename
The current filename being uploaded while an asynchronous upload is in progress.
topDebugLogFilePath
local strVal = upload.debug_log_file_path
upload.debug_log_file_path = strVal
If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.
Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.
Possible causes of hangs include:
- A timeout property set to 0, indicating an infinite timeout.
- A hang occurring within an event callback in the application code.
- An internal bug in the Chilkat code causing the hang.
Expect100Continue
local boolVal = upload.expect100_continue
upload.expect100_continue = boolVal
When true, the request header will included an Expect: 100-continue header field. This indicates that the server should respond with an intermediate response of 100 Continue or 417 Expectation Failed response based on the information available in the request header. This helps avoid situations such as limits on upload sizes. It allows the server to reject the upload, and then the client can abort prior to uploading the data.
The default value of this property is true.
HeartbeatMs
local intVal = upload.heartbeat_ms
upload.heartbeat_ms = intVal
The interval in milliseconds between each AbortCheck event callback, which enables an application to abort certain method calls before they complete. By default, HeartbeatMs is set to 0, meaning no AbortCheck event callbacks will trigger.
Hostname
local strVal = upload.hostname
upload.hostname = strVal
The hostname of the HTTP server that is the target of the upload. Do not include http:// in the hostname. It can be a hostname, such as www.chilkatsoft.com, or an IP address, such as 168.144.70.227.
IdleTimeoutMs
local intVal = upload.idle_timeout_ms
upload.idle_timeout_ms = intVal
A timeout in milliseconds. The default value is 30000. If the upload hangs (i.e. progress halts) for more than this time, the component will abort the upload. (It will timeout.)
topLastErrorHtml
local strVal = upload.last_error_html
Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastErrorText
local strVal = upload.last_error_text
Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
LastErrorXml
local strVal = upload.last_error_xml
Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastMethodSuccess
local boolVal = upload.last_method_success
upload.last_method_success = boolVal
Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.
Login
local strVal = upload.login
upload.login = strVal
The HTTP login for sites requiring authentication. Chilkat Upload supports Basic HTTP authentication.
topNumBytesSent
local intVal = upload.num_bytes_sent
After an upload has completed, this property contains the number of bytes sent. During asynchronous uploads, this property contains the current number of bytes sent while the upload is in progress.
topPassword
local strVal = upload.password
upload.password = strVal
The HTTP password for sites requiring authentication. Chilkat Upload supports Basic HTTP authentication.
topPath
local strVal = upload.path
upload.path = strVal
The path part of the upload URL. Some examples:
desc> If the upload target (i.e. consumer) URL is: http://www.freeaspupload.net/freeaspupload/testUpload.asp, then
Hostname =If the upload target URL is https://www.chilkatsoft.com/cgi-bin/ConsumeUpload.exe, thenwww.freeaspupload.netPath =/freeaspupload/testUpload.asp
Hostname =topwww.chilkatsoft.comPath =/cgi-bin/ConsumeUpload.exe
PercentDoneScale
local intVal = upload.percent_done_scale
upload.percent_done_scale = intVal
This property is only valid in programming environment and languages that allow for event callbacks.
Sets the value to be defined as 100% complete for the purpose of PercentDone event callbacks. The defaut value of 100 means that at most 100 event PercentDone callbacks will occur in a method that (1) is event enabled and (2) is such that it is possible to measure progress as a percentage completed. This property may be set to larger numbers to get more fine-grained PercentDone callbacks. For example, setting this property equal to 1000 will provide callbacks with .1 percent granularity. For example, a value of 453 would indicate 45.3% competed. This property is clamped to a minimum value of 10, and a maximum value of 100000.
topPercentUploaded
local intVal = upload.percent_uploaded
Contains the current percentage completion (0 to 100) while an asynchronous upload is in progress.
topPort
local intVal = upload.port
upload.port = intVal
The port number of the upload target (i.e. consumer) URL. The default value is 80. If SSL is used, this should be set to 443 (typically).
topPreferIpv6
local boolVal = upload.prefer_ipv6
upload.prefer_ipv6 = boolVal
If true, then use IPv6 over IPv4 when both are supported for a particular domain. The default value of this property is false, which will choose IPv4 over IPv6.
ProxyDomain
local strVal = upload.proxy_domain
upload.proxy_domain = strVal
The domain name of a proxy host if an HTTP proxy is used. Do not include the http://. The domain name may be a hostname, such as www.chilkatsoft.com, or an IP address, such as 168.144.70.227.
ProxyLogin
local strVal = upload.proxy_login
upload.proxy_login = strVal
If an HTTP proxy is used and it requires authentication, this property specifies the HTTP proxy login.
topProxyPassword
local strVal = upload.proxy_password
upload.proxy_password = strVal
If an HTTP proxy is used and it requires authentication, this property specifies the HTTP proxy password.
topProxyPort
local intVal = upload.proxy_port
upload.proxy_port = intVal
The port number of a proxy server if an HTTP proxy is used.
topResponseBodyStr
local strVal = upload.response_body_str
Returns the response body as a string.
topResponseHeader
local strVal = upload.response_header
An HTTP upload is nothing more than an HTTP POST that contains the content of the files being uploaded. Just as with any HTTP POST or GET, the server should send an HTTP response that consists of header and body.
desc>This property contains the header part of the HTTP response.
topResponseStatus
local intVal = upload.response_status
The HTTP response status code of the HTTP response. A list of HTTP status codes can be found here: HTTP Response Status Codes.
topSsl
local boolVal = upload.ssl
upload.ssl = boolVal
Set this to true if the upload is to HTTPS. For example, if the target of the upload is:
https://www.myuploadtarget.com/consumeUpload.asp
desc>then set:
Ssl =toptrue
Hostname =www.myuploadtarget.com
Path =/consumeupload.asp
Port = 443
SslAllowedCiphers
local strVal = upload.ssl_allowed_ciphers
upload.ssl_allowed_ciphers = strVal
Provides a means for setting a list of ciphers that are allowed for SSL/TLS connections. The default (empty string) indicates that all implemented ciphers are possible. The TLS ciphers supported in Chilkat v9.5.0.55 and later are:
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 TLS_DHE_RSA_WITH_AES_256_CBC_SHA TLS_RSA_WITH_AES_256_CBC_SHA256 TLS_RSA_WITH_AES_256_GCM_SHA384 TLS_RSA_WITH_AES_256_CBC_SHA TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 TLS_DHE_RSA_WITH_AES_128_CBC_SHA TLS_RSA_WITH_AES_128_CBC_SHA256 TLS_RSA_WITH_AES_128_GCM_SHA256 TLS_RSA_WITH_AES_128_CBC_SHA TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA TLS_RSA_WITH_3DES_EDE_CBC_SHA TLS_ECDHE_RSA_WITH_RC4_128_SHA TLS_RSA_WITH_RC4_128_SHA TLS_RSA_WITH_RC4_128_MD5 TLS_DHE_RSA_WITH_DES_CBC_SHA TLS_RSA_WITH_DES_CBC_SHATo restrict SSL/TLS connections to one or more specific ciphers, set this property to a comma-separated list of ciphers such as
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384. The order should be in terms of preference, with the preferred algorithms listed first. (Note that the client cannot specifically choose the algorithm is picked because it is the server that chooses. The client simply provides the server with a list from which to choose.)
The property can also disallow connections with servers having certificates with RSA keys less than a certain size. By default, server certificates having RSA keys of 512 bits or greater are allowed. Add the keyword rsa1024 to disallow connections with servers having keys smaller than 1024 bits. Add the keyword rsa2048 to disallow connections with servers having keys smaller than 2048 bits.
Note: Prior to Chilkat v9.5.0.55, it was not possible to explicitly list allowed cipher suites. The deprecated means for indicating allowed ciphers was both incomplete and unprecise. For example, the following keywords could be listed to allow matching ciphers: aes256-cbc, aes128-cbc, 3des-cbc, and rc4. These keywords will still be recognized, but programs should be updated to explicitly list the allowed ciphers.
secure-renegotiation: Starting in Chilkat v9.5.0.55, the keyword secure-renegotiation may be added to require that all renegotions be done securely (as per RFC 5746).
best-practices: Starting in Chilkat v9.5.0.55, this property may be set to the single keyword best-practices. This will allow ciphers based on the current best practices. As new versions of Chilkat are released, the best practices may change. Changes will be noted here. The current best practices are:
- If the server uses an RSA key, it must be 1024 bits or greater.
- All renegotations must be secure renegotiations.
- All ciphers using RC4, DES, or 3DES are disallowed.
Example: The following string would restrict to 2 specific cipher suites, require RSA keys to be 1024 bits or greater, and require secure renegotiations: TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, TLS_RSA_WITH_AES_256_CBC_SHA, rsa1024, secure-renegotiation
SslProtocol
local strVal = upload.ssl_protocol
upload.ssl_protocol = strVal
Selects the secure protocol to be used for secure (SSL/TLS) connections. Possible values are:
defaultThe default value is
TLS 1.3
TLS 1.2
TLS 1.1
TLS 1.0
SSL 3.0
TLS 1.3 or higher
TLS 1.2 or higher
TLS 1.1 or higher
TLS 1.0 or higher
default which will choose the, which allows for the protocol to be selected dynamically at runtime based on the requirements of the server. Choosing an exact protocol will cause the connection to fail unless that exact protocol is negotiated. It is better to choose X or higher than an exact protocol. The default is effectively SSL 3.0 or higher.
top
TlsPinSet
local strVal = upload.tls_pin_set
upload.tls_pin_set = strVal
Specifies a set of pins for Public Key Pinning for TLS connections. This property lists the expected SPKI fingerprints for the server certificates. If the server's certificate (sent during the TLS handshake) does not match any of the SPKI fingerprints, then the TLS handshake is aborted and the connection fails. The format of this string property is as follows:
hash_algorithm, encoding, SPKI_fingerprint_1, SPKI_fingerprint_2, ...For example, the following string specifies a single sha256 base64-encoded SPKI fingerprint:
"sha256, base64, lKg1SIqyhPSK19tlPbjl8s02yChsVTDklQpkMCHvsTE="This example specifies two SPKI fingerprints:
"sha256, base64, 4t37LpnGmrMEAG8HEz9yIrnvJV2euVRwCLb9EH5WZyI=, 68b0G5iqMvWVWvUCjMuhLEyekM5729PadtnU5tdXZKs="Any of the following hash algorithms are allowed:.sha1, sha256, sha384, sha512, md2, md5, haval, ripemd128, ripemd160,ripemd256, or ripemd320.
The following encodings are allowed: base64, hex, and any of the encodings indicated in the link below.
TotalUploadSize
local intVal = upload.total_upload_size
The total size of the upload (in bytes). This property will become set at the beginning of an asynchronous upload. A program may monitor asynchronous uploads by tracking both NumBytesSent and PercentUploaded.
desc> This property is also set during synchronous uploads.
topUploadInProgress
local boolVal = upload.upload_in_progress
Set to true when an asynchronous upload is started. When the asynchronous upload is complete, this property becomes equal to false. A program will typically begin an asynchronous upload by calling BeginUpload, and then periodically checking the value of this property to determine when the upload is complete.
UploadSuccess
local boolVal = upload.upload_success
Set to true (success) or false (failed) after an asynchronous upload completes or aborts due to failure. When a program does an asynchronous upload, it will wait until UploadInProgress becomes false. It will then check the value of this property to determine if the upload was successful or not.
VerboseLogging
local boolVal = upload.verbose_logging
upload.verbose_logging = boolVal
If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
local strVal = upload.version
Methods
AbortUpload
May be called during an asynchronous upload to abort.
topAddCustomHeader
-- value is a string
upload:add_custom_header(name, value)
Adds a custom HTTP header to the HTTP upload.
topAddFileReference
-- filename is a string
upload:add_file_reference(name, filename)
Adds a file to the list of files to be uploaded in the next call to BlockingUpload, BeginUpload, or UploadToMemory. To upload more than one file, call this method once for each file to be uploaded.
topAddParam
-- value is a string
upload:add_param(name, value)
Adds a custom HTTP request parameter to the upload.
topBeginUpload
local ok, err = upload:begin_upload()
Starts an asynchronous upload. Only one asynchronous upload may be in progress at a time. To achieve multiple asynchronous uploads, use multiple instances of the Chilkat Upload object. Each object instance is capable of managing a single asynchronous upload.
desc>
When this method is called, a background thread is started and the asynchronous upload runs in the background. The upload may be aborted at any time by calling AbortUpload. The upload is completed (or failed) when UploadInProgress becomes false. At that point, the UploadSuccess property may be checked to determine success (true) or failure (false).
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
BlockingUpload
local ok, err = upload:blocking_upload()
Uploads files to a web server using HTTP. The files to be uploaded are indicated by calling AddFileReference once for each file (prior to calling this method).
Returns true on success, nil, err on failure. The second return value, err, is the object's last_error_text; wrap the call in assert() to raise an error instead.
ClearFileReferences
Clears the internal list of files created by calls to AddFileReference.
topClearParams
Clears the internal list of params created by calls to AddParam.
topSleepMs
upload:sleep_ms(millisec)
A convenience method for putting the calling process to sleep for N milliseconds. It is provided here because some programming languages do not provide this capability, and sleeping for short increments of time is helpful when doing asynchronous uploads.
topEvents
All Chilkat methods are synchronous: the call returns when the work is done. During a long-running call, chilkat.Upload reports progress to the functions given to set_event_handler, in a table keyed by event name (plain functions, not methods: closures carry whatever state they need):
upload:set_event_handler({
percent_done = function(pct) print(pct .. "%") end, -- return true to abort
progress_info = function(name, value) print(name, value) end,
})
upload.heartbeat_ms = 250 -- abort_check is called 4 times per secondInstalls the handlers, replacing any installed before; upload:set_event_handler(nil) removes them. The event names are checked when the table is installed: a misspelled one raises an error.
The abort_check handler is called at the interval set by the heartbeat_ms property (0, the default, disables it); percent_done is called when an operation's completion percentage is known; progress_info delivers named progress values. A true return from abort_check or percent_done aborts the running method, which then fails in the usual way (nil, err). An error raised in a handler aborts the method and is raised again from the method call; the object remains usable.
AbortCheck
upload:set_event_handler({ abort_check = function() ... end })
Enables a method call to be aborted by triggering the AbortCheck event at intervals defined by the HeartbeatMs property. If HeartbeatMs is set to its default value of 0, no events will occur. For instance, set HeartbeatMs to 200 to trigger 5 AbortCheck events per second.
Example
local deadline = os.time() + 30 -- give up after 30 seconds
upload.heartbeat_ms = 250 -- call the handler 4 times per second
upload:set_event_handler({ abort_check = function() return os.time() > deadline end })PercentDone
upload:set_event_handler({ percent_done = function(pct_done) ... end })
This provides the percentage completion for any method involving network communications or time-consuming processing, assuming the progress can be measured as a percentage. This event is triggered only when it's possible and logical to express the operation's progress as a percentage. The pctDone argument will range from 1 to 100. For methods that finish quickly, the number of PercentDone callbacks may vary, but the final callback will have pctDone equal to 100. For longer operations, callbacks will not exceed one per percentage point (e.g., 1, 2, 3, ..., 98, 99, 100).
The PercentDone callback also acts as an AbortCheck event. For fast methods where PercentDone fires, an AbortCheck event may not trigger since the PercentDone callback already provides an opportunity to abort. For longer operations, where time between PercentDone callbacks is extended, AbortCheck callbacks enable more responsive operation termination.
To abort the operation, set the abort output argument to true. This will cause the method to terminate and return a failure status or corresponding failure value.
Example
upload:set_event_handler({
percent_done = function(pct_done) -- 0 to 100
print("Percent done: " .. pct_done)
return false -- return true to abort the method in progress
end,
})ProgressInfo
upload:set_event_handler({ progress_info = function(name, value) ... end })
This event callback provides tag name/value pairs that detail what occurs during a method call. To discover existing tag names, create code to handle the event, emit the pairs, and review them. Most tag names are self-explanatory.
Note: Some Chilkat methods don't fire any ProgressInfo events.
Example
upload:set_event_handler({
progress_info = function(name, value) print(name .. ": " .. value) end,
})