Total Pageviews

Thursday, April 17, 2014

Network Download

network.download()
វាជាប្រភេទហ្វាំងសិនដែល API នេះគឺងាយស្រួលដូចនឹង asynchronous network.request() លើកលែងតែវាព្យាយាមក្នុងការ download the response ទៅកាន់ local file។ វាប្រើវិធី network.request() ដោយដាក់ file name destination & baseDirectory parameters ទៅក្នុង params.response & specifying “download” progress notifications។ អ្នកអាចប្រើស display.loadRemoteImage() ក្នុងការ download & display a remote image នៅក្នុង single API call។ ហ្វាំងសិននេះត្រឡប់ជា handle ដែលនឹងឆ្លងទៅដល់ network.cancel() ក្នុងការចង់ cancel the request

ចំណាំះ មិនអាចឪ្យវាដំណើរការលើ network download អំឡុងពេល system Suspend/Exit Event។ បន្ទាប់ពី Corona suspends no callbacks will fire
សំរាប់ android: ដោយដាក់ការ​permission នៅក្នុង build.settings
settings =
{
   android =
   {
      usesPermissions =
      {
         "android.permission.INTERNET",
      },
   },
}

Syntax:
network.download( url, method, listener [, params], filename [, baseDirectory] )

url (required)

String. The HTTP request URL.


method (required)

String. The HTTP method; valid values are "GET" (the default) or "POST".


listener (required)

Listener. The listener function invoked at various phases of the HTTP operation. It is passed a networkRequest event. This API provides "download" progress when progress = true, meaning that the listener will receive events for "began", "progress", and "ended" phases. If the progress parameter is not enabled, the listener will only be invoked with an '"ended"' phase.


params (optional)

Table. A table that specifies HTTP request or response processing options, including custom request headers or body.
 •params.headers A table specifying request header values with string keys.
 •params.body A string containing the request body, if any.
 •params.timeout A time out in seconds. Default is 30 seconds.
 •params.bodyType A string indicating whether a string request body is "text" or "binary", default is "text".
 •params.progress Setting to true enables the progress events. Default is nil, indicating that only the "ended" phase event is desired.


filename (required)

String. The name of the file to which the response will be saved.


baseDirectory (optional)

Constant. The directory where the file will be saved. Defaults to system.DocumentsDirectory if not provided. Cannot be set to system.ResourceDirectory since that directory is read-only.


Examples

The following example downloads a remote image to a local file, and then displays it on the screen:
 local function networkListener( event )
        if ( event.isError ) then
                print( "Network error - download failed" )
        elseif ( event.phase == "began" ) then
                print( "Progress Phase: began" )
        elseif ( event.phase == "ended" ) then
                print( "displaying response image file" )
                myImage = display.newImage( event.response.filename, event.response.baseDirectory, 60, 40 )
                myImage.alpha = 0
                transition.to( myImage, { alpha = 1.0 } )
        end
end

local params = {}
        params.progress = true

network.download(
        "http://www.coronalabs.com/demo/hello.png",
        "GET",
        networkListener,
        params,
        "helloCopy.png",
        system.TemporaryDirectory
        )


No comments:

Post a Comment