Skip to content

Instantly share code, notes, and snippets.

@Abdelhady
Last active November 17, 2018 01:04
Show Gist options
  • Save Abdelhady/6dffed23e696f8a335926cfa114b2414 to your computer and use it in GitHub Desktop.
Save Abdelhady/6dffed23e696f8a335926cfa114b2414 to your computer and use it in GitHub Desktop.
Initial Tus implementation
var tus = require("tus-js-client");
$('#uploadBtn').change(function () {
var file = this.files[0];
if (typeof file === "undefined") {
return;
}
// some validations goes here using `file.size` & `file.type`
var upload = new tus.Upload(file, {
// https://github.com/tus/tus-js-client#tusdefaultoptions
endpoint: "/tus",
retryDelays: [0, 1000, 3000, 5000, 10000],
metadata: {
filename: file.name,
filetype: file.type
},
onError: function (error) {
// Handle errors here
},
onProgress: function (bytesUploaded, bytesTotal) {
// Reflect values on your progress bar using `bytesTotal` & `bytesUploaded`
},
onSuccess: function () {
// Upload has been completed
}
});
// Start the upload
upload.start();
});
<?php
class TusController {
public function indexAction() {
// Disable views/layout the way suites your framework
$server = new TusPhp\Tus\Server(); // Using File Cache (over Redis) for simpler setup
$server->setApiPath('/tus/index') // tus server endpoint.
->setUploadDir('/tmp'); // uploads dir.
$response = $server->serve();
$response->send();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment