NativeScript plugin for TUS - Open Protocol for Resumable File Uploads. Uses TUSKit (iOS) and tus-android-client.
iOS | Android |
---|---|
tns plugin add nativescript-tus-client
import { File, Folder, knownFolders } from "@nativescript/core";
import * as tus from "nativescript-tus-client";
// create a File reference
const file = File.fromPath(knownFolders.currentApp().path + 'assets/test_image.png');
// Create a new tus upload
var upload = new tus.Upload(file, {
endpoint: "http://192.168.1.118:1080/files/",
metadata: {
filename: 'test_image.png',
filetype: 'image/png'
},
headers: {
Authorization: "Basic YWxhZGRpbjpvcGVuc2VzYW1l"
},
onError: function(error) {
console.log("Failed because: " + error)
},
onProgress: function(bytesUploaded, bytesTotal) {
var percentage = (bytesUploaded / bytesTotal * 100).toFixed(2)
console.log(bytesUploaded, bytesTotal, percentage + "%")
},
onSuccess: function() {
console.log(`Download ${upload.file.name} from ${upload.url}` )
}
})
// Start the upload
upload.start()
// Abort the upload
upload.abort()
nativesctipt-tus-plugin
implements a very limited subset of the JS api. See the example above as it contains all the implemented API.
We included a tus test server. Remember to change the host in test-server/index.js
:
const host = '192.168.1.118';
then:
cd test-server
npm run start
You will have to change the host in the demo app at demo/app/home/home-page.ts
too.
A big thanks to coderReview for leading the way with his nativescript-tus-upload.
Apache License Version 2.0, January 2004