Lua
Lua
Download Image (JPG, GIF, etc.) to Base64
See more HTTP Examples
Demonstrates how to download an image, or any type of file, to get the data in base64 encoding format.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
http.keep_response_body = true
local bd = chilkat.BinData.new()
local success = http:download_bd("https://www.chilkatsoft.com/images/starfish.jpg", bd)
local statusCode = http.last_status
if not success then
if statusCode == 0 then
-- Unable to either send the request or get the response.
print(http.last_error_text)
else
-- We got a response, but the status code was not in the 200s
print("Response status code: " .. statusCode)
-- Examine the response body.
print("Response body:")
print(http.last_response_body)
end
print("Download failed.")
else
print("Download success, response status = " .. statusCode)
local base64_image_data = bd:get_encoded("base64")
print("image data in base64 format:")
print(base64_image_data)
end