Difference between revisions of "UrlLoader"
Line 2: | Line 2: | ||
'''Supported platforms:''' android, ios, mac, pc<br/> | '''Supported platforms:''' android, ios, mac, pc<br/> | ||
'''Available since:''' Gideros 2012.2.2<br/> | '''Available since:''' Gideros 2012.2.2<br/> | ||
− | === Description === | + | === <translate>Description</translate> === |
<translate><br /> | <translate><br /> | ||
The [[Special:MyLanguage/UrlLoader|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.<br /> | The [[Special:MyLanguage/UrlLoader|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.<br /> | ||
Line 18: | Line 18: | ||
</ul><br /> | </ul><br /> | ||
<br /></translate> | <br /></translate> | ||
− | === Examples === | + | === <translate>Examples</translate> === |
'''The example below shows downloading an image file from an URL, saving it to the documents folder and displaying it<br /> | '''The example below shows downloading an image file from an URL, saving it to the documents folder and displaying it<br /> | ||
on the stage. This example also shows downloading progress and handling errors.<br /> | on the stage. This example also shows downloading progress and handling errors.<br /> | ||
Line 69: | Line 69: | ||
{|- | {|- | ||
| style="width: 50%; vertical-align:top;"| | | style="width: 50%; vertical-align:top;"| | ||
− | === Methods === | + | === <translate>Methods</translate> === |
[[Special:MyLanguage/UrlLoader.new|UrlLoader.new]] ''<translate>creates a new UrlLoader object</translate>''<br/> | [[Special:MyLanguage/UrlLoader.new|UrlLoader.new]] ''<translate>creates a new UrlLoader object</translate>''<br/> | ||
[[Special:MyLanguage/UrlLoader:close|UrlLoader:close]] ''<translate>terminates the current loading operation</translate>''<br/> | [[Special:MyLanguage/UrlLoader:close|UrlLoader:close]] ''<translate>terminates the current loading operation</translate>''<br/> | ||
Line 75: | Line 75: | ||
[[Special:MyLanguage/UrlLoader:load|UrlLoader:load]] ''<translate>loads data from the specified URL</translate>''<br/> | [[Special:MyLanguage/UrlLoader:load|UrlLoader:load]] ''<translate>loads data from the specified URL</translate>''<br/> | ||
| style="width: 50%; vertical-align:top;"| | | style="width: 50%; vertical-align:top;"| | ||
− | === Events === | + | === <translate>Events</translate> === |
[[Special:MyLanguage/Event.COMPLETE|Event.COMPLETE]]<br/> | [[Special:MyLanguage/Event.COMPLETE|Event.COMPLETE]]<br/> | ||
[[Special:MyLanguage/Event.ERROR|Event.ERROR]]<br/> | [[Special:MyLanguage/Event.ERROR|Event.ERROR]]<br/> | ||
[[Special:MyLanguage/Event.PROGRESS|Event.PROGRESS]]<br/> | [[Special:MyLanguage/Event.PROGRESS|Event.PROGRESS]]<br/> | ||
− | === Constants === | + | === <translate>Constants</translate> === |
[[Special:MyLanguage/UrlLoader.DELETE|UrlLoader.DELETE]]<br/> | [[Special:MyLanguage/UrlLoader.DELETE|UrlLoader.DELETE]]<br/> | ||
[[Special:MyLanguage/UrlLoader.GET|UrlLoader.GET]]<br/> | [[Special:MyLanguage/UrlLoader.GET|UrlLoader.GET]]<br/> |
Revision as of 07:28, 24 August 2018
Supported platforms: android, ios, mac, pc
Available since: Gideros 2012.2.2
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:
Examples
The example below shows 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,
["Content-Length"] = #send,
}
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 |
EventsEvent.COMPLETE Constants |