A cross platform plugin for the NativeScript framework, that provides background upload for iOS and Android.
There is a stock NativeScript http
module that can handle GET/POST requests that work with strings and JSONs. It however comes short in features when it comes to really large files.
The plugin uses NSURLSession with background session configuration for iOS; and for android - the alexbbb/android-upload-service that implements an IntentService.
tns plugin add nativescript-background-http
The below attached code snippets demonstrate how to use nativescript-background-http
, while uploading single or multiple files.
uploading sigle file to the service
import * as bghttp from "nativescript-background-http";
var session = bghttp.session("image-upload");
.....
var request = {
url: url,
method: "POST",
headers: {
"Content-Type": "application/octet-stream",
"File-Name": name
},
description: description
};
if (should_fail) {
request.headers["Should-Fail"] = true;
}
let task: bghttp.Task;
task = session.uploadFile(file, request);
uploading multiple files while using nativescript-background-http
. Make sure all params sent to multipartUpload
have string values.
import * as bghttp from "nativescript-background-http";
var session = bghttp.session("image-upload");
.....
var request = {
url: url,
method: "POST",
headers: {
"Content-Type": "application/octet-stream",
"File-Name": name
},
description: description
};
if (should_fail) {
request.headers["Should-Fail"] = true;
}
let task: bghttp.Task;
var params = [
{ name: "test", value: "value" },
{ name: "fileToUpload", filename: file, mimeType: 'image/jpeg' }
];
task = session.multipartUpload(params, request);
To get advantage of the demo apps and test the functionality, you will have to start the server from
demo-server
folder part of the repository. To do so, executecd demo-server && npm run start
command in a terminal.
We love PRs! Check out the contributing guidelines. If you want to contribute, but you are not sure where to start - look for issues labeled help wanted
.
Please, use github issues strictly for reporting bugs or requesting features. For general questions and support, check out the NativeScript community forum or ask our experts in NativeScript community Slack channel.