Difference between revisions of "UrlLoader"
(3 intermediate revisions by 3 users not shown) | |||
Line 16: | Line 16: | ||
*''UrlLoader.PUT = "put"'' | *''UrlLoader.PUT = "put"'' | ||
*''UrlLoader.DELETE = "delete"'' | *''UrlLoader.DELETE = "delete"'' | ||
+ | |||
+ | ==== HTTPS support on Windows ==== | ||
+ | To enable https support on windows additional files are required. [https://forum.giderosmobile.com/discussion/comment/66294/#Comment_66294 See this forum post]. | ||
=== Examples === | === Examples === | ||
'''Downloading an image file from an URL, saving it to the documents folder and displaying it on the stage. This example also shows downloading progress and handling errors''': | '''Downloading an image file from an URL, saving it to the documents folder and displaying it on the stage. This example also shows downloading progress and handling errors''': | ||
− | < | + | <syntaxhighlight lang="lua"> |
local loader = UrlLoader.new("http://example.com/image.png") | local loader = UrlLoader.new("http://example.com/image.png") | ||
Line 42: | Line 45: | ||
loader:addEventListener(Event.ERROR, onError) | loader:addEventListener(Event.ERROR, onError) | ||
loader:addEventListener(Event.PROGRESS, onProgress) | loader:addEventListener(Event.PROGRESS, onProgress) | ||
− | </ | + | </syntaxhighlight> |
'''Uploading a file''': | '''Uploading a file''': | ||
− | < | + | <syntaxhighlight lang="lua"> |
local filename = "crate.png" | local filename = "crate.png" | ||
local file = io.open(filename, "rb") | local file = io.open(filename, "rb") | ||
Line 67: | Line 70: | ||
print(e.data) | print(e.data) | ||
end) | end) | ||
− | </ | + | </syntaxhighlight> |
{|- | {|- | ||
Line 76: | Line 79: | ||
[[UrlLoader:ignoreSslErrors]] ''ignores SSL certificate related errors''<br/><!--GIDEROSMTD:UrlLoader:ignoreSslErrors() ignores SSL certificate related errors--> | [[UrlLoader:ignoreSslErrors]] ''ignores SSL certificate related errors''<br/><!--GIDEROSMTD:UrlLoader:ignoreSslErrors() ignores SSL certificate related errors--> | ||
[[UrlLoader:load]] ''loads data from the specified URL''<br/><!--GIDEROSMTD:UrlLoader:load(url,method,headers,body) loads data from the specified URL--> | [[UrlLoader:load]] ''loads data from the specified URL''<br/><!--GIDEROSMTD:UrlLoader:load(url,method,headers,body) loads data from the specified URL--> | ||
+ | [[UrlLoader:setStreaming]] ''enables streaming mode''<br/><!--GIDEROSMTD:UrlLoader:setStreaming(bool) enables streaming mode--> | ||
| style="width: 50%; vertical-align:top;"| | | style="width: 50%; vertical-align:top;"| |
Latest revision as of 22:28, 9 October 2023
Supported platforms:
Available since: Gideros 2012.2.2
Inherits from: Object
Description
The UrlLoader Class is used to download data from an URL. It can be used to download (and optionally save) text files, XML files, JSON files, image files or binary files, etc.
Downloaded data is delivered at event.data field of Event.COMPLETE event as string. Lua is eight-bit clean and so strings may contain characters with any numeric value, including embedded zeros. That means that you can store any binary data into a string.
HTTP Request Methods
UrlLoader supports GET, POST, PUT and DELETE methods. These are defined by these string constants:
- UrlLoader.GET = "get"
- UrlLoader.POST = "post"
- UrlLoader.PUT = "put"
- UrlLoader.DELETE = "delete"
HTTPS support on Windows
To enable https support on windows additional files are required. See this forum post.
Examples
Downloading an image file from an URL, saving it to the documents folder and displaying it on the stage. This example also shows downloading progress and handling errors:
local loader = UrlLoader.new("http://example.com/image.png")
local function onComplete(event)
local out = io.open("|D|image.png", "wb")
out:write(event.data)
out:close()
local b = Bitmap.new(Texture.new("|D|image.png"))
stage:addChild(b)
end
local function onError()
print("error")
end
local function onProgress(event)
print("progress: " .. event.bytesLoaded .. " of " .. event.bytesTotal)
end
loader:addEventListener(Event.COMPLETE, onComplete)
loader:addEventListener(Event.ERROR, onError)
loader:addEventListener(Event.PROGRESS, onProgress)
Uploading a file:
local filename = "crate.png"
local file = io.open(filename, "rb")
local contents = file:read( "*a" )
local boundary = "somerndstring"
local send = "--"..boundary..
"\r\nContent-Disposition: form-data; "..
"name="..filename.."; filename="..filename..
"\r\nContent-type: image/png"..
"\r\n\r\n"..contents..
"\r\n--"..boundary.."--\r\n";
local headers = {
["Content-Type"] = "multipart/form-data; boundary="..boundary,
}
local loader = UrlLoader.new("http://localhost/gideros.php", UrlLoader.POST, headers, send)
loader:addEventListener(Event.COMPLETE, function(e)
print(e.data)
end)
MethodsUrlLoader.new creates a new UrlLoader object |
EventsUrlLoader_Event.COMPLETE Constants |