Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>tus-js-client demo - File Upload</title>
    <link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet" />
    <link href="./demo.css" rel="stylesheet" media="screen" />
  </head>
  <body>
    <div class="container">
      <h1>tus-js-client demo - File Upload</h1>

      <p>
        This demo shows the basic functionality of the tus protocol. You can select a file using the controls below and start/pause the upload as you wish.
      </p>

      <p>
        For a prettier demo please go to the
        <a href="http://tus.io/demo.html">tus.io</a> website.
        This demo is just here to aid developers.
      </p>

      <p>
        A demo where a video is recorded from your webcam while being simultaneously uploaded, can be found <a href="./video.html">here</a>.
      </p>

      <div class="alert alert-warining hidden" id="support-alert">
        <b>Warning!</b> Your browser does not seem to support the features necessary to run tus-js-client. The buttons below may work but probably will fail silently.
      </div>

      <br />

      <table>
        <tr>
          <td>
            <label for="endpoint">
              Upload endpoint:
            </label>
          </td>
          <td>
            <input type="text" id="endpoint" name="endpoint" value="https://restless-resonance-fd00.zaid-stream-demo.workers.dev/">
          </td>
        </tr>
        <tr>
          <td>
            <label for="chunksize">
              Chunk size (bytes):
            </label>
          </td>
          <td>
            <input type="number" id="chunksize" name="chunksize">
          </td>
        </tr>
        <tr>
          <td>
            <label for="paralleluploads">
              Parallel upload requests:
            </label>
          </td>
          <td>
            <input type="number" id="paralleluploads" name="paralleluploads" value="1">
          </td>
        </tr>
      </table>

      <br />

      <input type="file">

      <br />
      <br />

      <div class="row">
        <div class="span8">
          <div class="progress progress-striped progress-success">
            <div class="bar" style="width: 0%;"></div>
          </div>
        </div>
        <div class="span4">
          <button class="btn stop" id="toggle-btn">start upload</button>
        </div>
      </div>

      <hr />
      <h3>Uploads</h3>
      <p id="upload-list">
        Succesful uploads will be listed here. Try one!
      </p>

    </div>
  </body>

  <script src="https://cdn.jsdelivr.net/npm/tus-js-client@2.1.1/dist/tus.js"></script>
  <script src="./demo.js"></script>
</html>

              
            
!

CSS

              
                body {
	padding-top: 40px;
}

.progress {
	height: 32px;
}

a.btn {
	margin-bottom: 2px;
}

              
            
!

JS

              
                /* global tus */
/* eslint no-console: 0 */

"use strict";

var upload          = null;
var uploadIsRunning = false;
var toggleBtn       = document.querySelector("#toggle-btn");
var input           = document.querySelector("input[type=file]");
var progress        = document.querySelector(".progress");
var progressBar     = progress.querySelector(".bar");
var alertBox        = document.querySelector("#support-alert");
var uploadList      = document.querySelector("#upload-list");
var chunkInput      = document.querySelector("#chunksize");
var parallelInput   = document.querySelector("#paralleluploads");
var endpointInput   = document.querySelector("#endpoint");

if (!tus.isSupported) {
  alertBox.classList.remove("hidden");
}

if (!toggleBtn) {
  throw new Error("Toggle button not found on this page. Aborting upload-demo. ");
}

toggleBtn.addEventListener("click", function (e) {
  e.preventDefault();

  if (upload) {
    if (uploadIsRunning) {
      upload.abort();
      toggleBtn.textContent = "resume upload";
      uploadIsRunning = false;
    } else {
      upload.start();
      toggleBtn.textContent = "pause upload";
      uploadIsRunning = true;
    }
  } else {
    if (input.files.length > 0) {
      startUpload();
    } else {
      input.click();
    }
  }
});

input.addEventListener("change", startUpload);

function startUpload() {
  var file = input.files[0];
  // Only continue if a file has actually been selected.
  // IE will trigger a change event even if we reset the input element
  // using reset() and we do not want to blow up later.
  if (!file) {
    return;
  }

  var endpoint = endpointInput.value;
  var chunkSize = parseInt(chunkInput.value, 10);
  alert("Chunk size " + chunkSize);
  if (isNaN(chunkSize)) {
    chunkSize = Infinity;
  }

  var parallelUploads = parseInt(parallelInput.value, 10);
  if (isNaN(parallelUploads)) {
    parallelUploads = 1;
  }

  toggleBtn.textContent = "pause upload";

  var options = {
    endpoint: endpoint,
    chunkSize: chunkSize,
    retryDelays: [0, 1000, 3000, 5000],
    parallelUploads: parallelUploads,
    metadata: {
      name: file.name,
      internal_user_id: 123
    },
    onError : function (error) {
      if (error.originalRequest) {
        if (window.confirm("Failed because: " + error + "\nDo you want to retry?")) {
          upload.start();
          uploadIsRunning = true;
          return;
        }
      } else {
        window.alert("Failed because: " + error);
      }

      reset();
    },
    onProgress: function (bytesUploaded, bytesTotal) {
      var percentage = (bytesUploaded / bytesTotal * 100).toFixed(2);
      progressBar.style.width = percentage + "%";
      console.log(bytesUploaded, bytesTotal, percentage + "%");
    },
    onSuccess: function () {
      var anchor = document.createElement("a");
      anchor.textContent = "Download " + upload.file.name + " (" + upload.file.size + " bytes)";
      anchor.href = upload.url;
      anchor.className = "btn btn-success";
      uploadList.appendChild(anchor);

      reset();
    }
  };

  upload = new tus.Upload(file, options);
  upload.findPreviousUploads().then((previousUploads) => {
    askToResumeUpload(previousUploads, upload);

    upload.start();
    uploadIsRunning = true;
  });

}

function reset() {
  input.value = "";
  toggleBtn.textContent = "start upload";
  upload = null;
  uploadIsRunning = false;
}


function askToResumeUpload(previousUploads, upload) {
  if (previousUploads.length === 0) return;

  let text = "You tried to upload this file previously at these times:\n\n";
  previousUploads.forEach((previousUpload, index) => {
    text += "[" + index + "] " + previousUpload.creationTime + "\n";
  });
  text += "\nEnter the corresponding number to resume an upload or press Cancel to start a new upload";

  const answer = prompt(text);
  const index = parseInt(answer, 10);

  if (!isNaN(index) && previousUploads[index]) {
    upload.resumeFromPreviousUpload(previousUploads[index]);
  }
}

              
            
!
999px

Console