GitHub - kiwiirc/nativescript-tus-client (original) (raw)
nativescript-tus-client
NativeScript plugin for TUS - Open Protocol for Resumable File Uploads. Uses TUSKit (iOS) and tus-android-client.
iOS | Android |
---|---|
![]() |
![]() |
Installation
tns plugin add nativescript-tus-client
Usage
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 <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>u</mi><mi>p</mi><mi>l</mi><mi>o</mi><mi>a</mi><mi>d</mi><mi mathvariant="normal">.</mi><mi>f</mi><mi>i</mi><mi>l</mi><mi>e</mi><mi mathvariant="normal">.</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mrow><mi>f</mi><mi>r</mi><mi>o</mi><mi>m</mi></mrow><annotation encoding="application/x-tex">{upload.file.name} from </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal">u</span><span class="mord mathnormal" style="margin-right:0.01968em;">pl</span><span class="mord mathnormal">o</span><span class="mord mathnormal">a</span><span class="mord mathnormal">d</span><span class="mord">.</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span><span class="mord">.</span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span></span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">ro</span><span class="mord mathnormal">m</span></span></span></span>{upload.url}
)
}
})
// Start the upload upload.start()
// Abort the upload upload.abort()
API
nativesctipt-tus-plugin
implements a very limited subset of the JS api. See the example above as it contains all the implemented API.
Test server
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.
Credits
A big thanks to coderReview for leading the way with his nativescript-tus-upload.
License
Apache License Version 2.0, January 2004