File(s) upload (v5)
How to upload file(s) from SDK >= 5.0.1
The following information explain how to implement files upload from the SDK.
In order to use the file upload feature, you need to update at least the SDK to 5.0.1.
By default, the file upload web service reach this url: https://" +appName + ".apnl.ws/files/upload
But it's possible to set a specific one !
With uploadFile(s)
The easiest way to upload one or more files is to use the uploadFile-s methods.
For one file, the function uploadFile require at least 3 parameters :
- the name of the file to upload
- the file to upload
- the callback listener
You can upload several files by using uploadFiles which require at least 2 parameters:
- a map list associating filename and file
- the callback listener
But you can give more attributes
- additionnal query parameters
- additionnal query headers
- a specific url for uploading service
//Optional additional parameters
val params = HashMap<String, String>()
params["key1"] = "value1"
params["key2"] = "value2"
//Optional API URL customisation
val url = "https://custom-ws-url/upload"
//Method to send one file...
APWSManager.uploadFile("myfilename",
file,
params,
url,
object : UploadListener {
override fun onResponse(response: String) {
Log.d("DEV", "onResponse $response")
}
override fun onError(error: String) {
Log.d("DEV", "onError $error")
}
})
//...or with several files
val files: MutableMap<String, File> = HashMap()
files["filename"] = file1
files["filename2"] = file2
APWSManager.uploadFiles(files,
params,
url,
object : UploadListener {
override fun onResponse(response: String) {
Log.d("DEV", "onResponse $response")
}
override fun onError(error: String) {
Log.d("DEV", "onError $error")
}
})
With these methods the token is sent by default as query parameter if it is previously setted in the SDK local configuration :
APManager.instance.localConfiguration.userToken = "user_token"
With request
An other way more similar to standard query is to build the file request like below
val req = FileRequest(
object : UploadListener {
override fun onResponse(response: String) {
Log.d("DEV", "onResponse $response")
}
override fun onError(error: String) {
Log.d("DEV", "onError $error")
}
})
//Optional additional parameter
req.addParam("key", "value")
//or several
val params = HashMap<String, String>()
params["key1"] = "value1"
params["key2"] = "value2"
req.addParams(params)
//Optional additional headers
val headers = HashMap<String, String>()
headers["key1"] = "value1"
headers["key2"] = "value2"
req.headers = headers
//Add one...
req.addFile("filename", file)
//...or several files
val files: MutableMap<String, File> = HashMap()
files["filename"] = file1
files["filename2"] = file2
req.addFiles(files)
//Optional API URL customisation
req.url = "https://custom-ws-url/upload" // param is uploadAPI for SDK <= 5.0.3
//Optional user token sending
req.sendUserToken = true
//Optional upload timeout
req.timeout = 60
//Optional manually define the content type of file
req.contentType = "app/toto"
APWSManager.doRequest(req)
With these methods the token is sent as query parameter if it was previously setted in the SDK local configuration AND if the request attribute sendUserToken is setted as TRUE
APManager.instance.localConfiguration.userToken = "user_token"
//...
req.sendUserToken = true
You can add more attributes to your request as
- a specific contentType to manually set the mime type of your file, determined from extension instead
- a specific timeout
Updated over 2 years ago