Upload

Upload files through a file input form element or a placeholder area.

Usage

This JavaScript component utilizes the latest XMLHttpRequest Level 2 specification and provides the ability to upload files via Ajax including tracking of the upload progress. The component provides two ways of uploading files: select and drop. While the select request can only be applied to <input type="file"> elements, you can basically use any element with drop, which enables you to simply drag and drop files from your desktop into the specified element to upload them. Note that this component does not handle your file uploads on the server.

Select

In this example we are using a simple button which opens up the file select window.

Copy to clipboard
  • <div class="js-upload" uk-form-custom>
      <input type="file" multiple />
      <button class="uk-button uk-button-default" type="button" tabindex="-1">
        Select
      </button>
    </div>

Drop area

This example shows how to realize a drop area with the option to select the file from a file window.

Copy to clipboard
  • Attach binaries by dropping them here or
    selecting one
  • <div class="js-upload uk-placeholder uk-text-center">
      <span class="uk-text-middle">Attach binaries by dropping them here or</span>
      <div uk-form-custom>
        <input type="file" multiple />
        <span class="uk-link">selecting one</span>
      </div>
    </div>
    
    <progress
      id="js-progressbar"
      class="uk-progress"
      value="0"
      max="100"
      hidden
    ></progress>
    
    <script>
      var bar = document.getElementById("js-progressbar");
    
      UIkit.upload(".js-upload", {
        url: "",
        multiple: true,
    
        beforeSend: function () {
          console.log("beforeSend", arguments);
        },
        beforeAll: function () {
          console.log("beforeAll", arguments);
        },
        load: function () {
          console.log("load", arguments);
        },
        error: function () {
          console.log("error", arguments);
        },
        complete: function () {
          console.log("complete", arguments);
        },
    
        loadStart: function (e) {
          console.log("loadStart", arguments);
    
          bar.removeAttribute("hidden");
          bar.max = e.total;
          bar.value = e.loaded;
        },
    
        progress: function (e) {
          console.log("progress", arguments);
    
          bar.max = e.total;
          bar.value = e.loaded;
        },
    
        loadEnd: function (e) {
          console.log("loadEnd", arguments);
    
          bar.max = e.total;
          bar.value = e.loaded;
        },
    
        completeAll: function () {
          console.log("completeAll", arguments);
    
          setTimeout(function () {
            bar.setAttribute("hidden", "hidden");
          }, 1000);
    
          alert("Upload Completed");
        },
      });
    </script>

Upload listeners

To create select and drop upload listeners, you need to instantiate each upload class with the target element and options, which define callbacks and useful settings.

<script>
  var bar = document.getElementById("js-progressbar");

  UIkit.upload(".js-upload", {
    url: "",
    multiple: true,

    beforeSend: function (environment) {
      console.log("beforeSend", arguments);

      // The environment object can still be modified here.
      // var {data, method, headers, xhr, responseType} = environment;
    },
    beforeAll: function () {
      console.log("beforeAll", arguments);
    },
    load: function () {
      console.log("load", arguments);
    },
    error: function () {
      console.log("error", arguments);
    },
    complete: function () {
      console.log("complete", arguments);
    },

    loadStart: function (e) {
      console.log("loadStart", arguments);

      bar.removeAttribute("hidden");
      bar.max = e.total;
      bar.value = e.loaded;
    },

    progress: function (e) {
      console.log("progress", arguments);

      bar.max = e.total;
      bar.value = e.loaded;
    },

    loadEnd: function (e) {
      console.log("loadEnd", arguments);

      bar.max = e.total;
      bar.value = e.loaded;
    },

    completeAll: function () {
      console.log("completeAll", arguments);

      setTimeout(function () {
        bar.setAttribute("hidden", "hidden");
      }, 1000);

      alert("Upload Completed");
    },
  });
</script>

Component options

Any of these options can be applied to the component attribute. Separate multiple options with a semicolon. Learn more

OptionValueDefaultDescription
urlStringThe request url.
multipleBooleanfalseAllow multiple files to be uploaded.
nameStringfiles[]The name parameter.
typeStringPOSTThe request type.
paramsObject{}Additional parameters.
allowStringfalseFile name filter. (eg. *.png)
mimeStringfalseFile MIME type filter. (eg. image/*)
concurrentNumber1Number of files that will be uploaded simultaneously.
typeStringThe expected response data type (xml, json, script, or html)
methodStringPOSTThe request method
msg-invalid-mimeStringInvalid File Type: %sInvalid MIME type message.
msg-invalid-nameStringInvalid File Name: %sInvalid name message.
cls-dragoverStringuk-dragoverFile name filter.
abortFunctionThe abort callback.
before-allFunctionThe beforeAll callback.
before-sendFunctionThe beforeSend callback.
completeFunctionThe complete callback.
complete-allFunctionThe completeAll callback.
errorFunctionThe error callback.
loadFunctionThe load callback.
load-endFunctionThe loadEnd callback.
load-startFunctionThe loadStart callback.
progressFunctionThe progress callback.
failFunctionThe fail callback. If the name or MIME type is invalid.

JavaScript

Learn more about JavaScript components.

Initialization

UIkit.upload(element, options);

Events

The following events will be triggered on elements with this component attached:

NameDescription
uploadFires before files are being uploaded.
Customize