Total Pageviews

Thursday, April 17, 2014

Network Upload

network.upload()
សំរាប់ API មួយនេះគឺងាយស្រួលក្នុងការ asynchronous network.request() លើកលែងតែព្យាយាមក្នុងការ upload the request ចេញពី local file។ វានឹងហៅ network.request() ដោយដាក់ source filename & baseDirectory parameters ទៅក្នុង params.body table, ការបន្ថែម contentType ជា params.headers ដែលសំនូមពរ header value ហើយ “upload” progress notifications។ សំរាប់ហ្វាំងសិននេះត្រឡប់ជា handle ដែលបញ្ចូល network.cancel() ក្នុងការ cancel request
សំរាប់ android ត្រូវការដាក់ permission នៅក្នុង build.settings

settings =
{
   android =
   {
      usesPermissions =
      {
         "android.permission.INTERNET",
      },
   },
}

Syntax
 network.upload( url, method, listener [, params], filename [, baseDirectory] [, contentType] )

url (required)

String. The HTTP request URL.


method (required)

String. The HTTP method; should typically be either "PUT" or "POST" for a file upload.


listener (required)

Listener. The listener function invoked at various phases of the HTTP operation. It is passed a networkRequest event. This API provides "upload" 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 processing options, including custom request headers.
 •params.headers A table specifying header values with string keys.
 •params.timeout A time out in seconds. Default is 30 seconds. (Available on Mac/iOS)
 •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 upload.


baseDirectory (optional)

Constant. The directory of the file to upload. Defaults to system.DocumentsDirectory if not provided.


contentType (optional)

Constant. The Content-Type of the file being uploaded. While this value is optional, a Content-Type must be provided, either via this parameter, or via a params.header value.


Examples

The following code demonstrates an HTTP post, where the request body comes from a file.

Note: This example demonstrates one possible way to upload a file. Different web servers and applications support different methods of file upload. For example, some REST services support upload of a binary file in the body of a PUT request. Many web servers only allow file uploads using multipart/form-encoded (text) request bodies in a POST request. If you are going to attempt file upload, you must first understand the specific mechanism supported by the specific web server or application that you will be working with, and then you must form your request data and choose your request method appropriately.
 local function networkListener( event )
        if ( event.isError ) then
                print( "Network error!")
        elseif ( event.phase == "ended" ) then
                print ( "Upload complete!" )
        end
end

network.upload(
        "http://127.0.0.1/restapi.php",
        "POST",
        networkListener,
        "object.json",
        system.DocumentsDirectory,
        "application/json"

        )

No comments:

Post a Comment