FileUpload - Documentation | www.HtmlElements.com

Overview

Smart.FileUpload is an element which can be used to select files and upload them to a server. User can upload single file, multiple files, directories(supported in Firefox and Google Chrome). The element allows file upload by dropping files on custom surface.

Files

  • CSS files
    • smart.default.css - the CSS file containing the styles for the element.
  • Javascript module
    • smart.fileupload.js - the JS module which is in the source/modules/ folder and loads all script dependencies.
  • Javascript files
    • smart.element.js – the base class
    • smart.button.js – the JS file containing the definition for the buttons.
    • smart.fileupload.js – the JS file containing the element definition

Usage

  • Import a module

    With this approach, we import a module and create the web component by using the Smart function. The #fileUpload is a smart-file-upload tag.

    import {smartFileUpload} from "../../source/modules/smart.fileupload.js";
    
    Smart('#fileUpload', class {
    	get properties() {
    		return {
    			multiple: true
    		}
    	}
    });
    
    document.readyState === 'complete' ? init() : window.onload = init;
    
    function init() { 
    
    }
    
    Using the Smart function is optional. You can use const fileUpload = document.querySelector("#fileUpload"); and set the properties like that:
    const fileUpload = document.querySelector("#component");
    fileUpload.multiple = true;
    

  • Import a module, Init on Demand
    The following imports the web component's module and creates it on demand, when the document is ready. The #fileUpload is a DIV tag.
    import {smartFileUpload} from "../../source/modules/smart.fileupload.js";
    
    document.readyState === 'complete' ? init() : window.onload = init;
    
    function init() { 
        const component = new smartFileUpload('#fileUpload', {
          
        });
    }
    
  • Load scripts

    The following code adds File Upload element to the page.

    <!DOCTYPE html>
    <html lang="en">
    <head>
     <link rel="stylesheet" href="../../source/styles/smart.base.css" type="text/css" />
     <script type="text/javascript" src="../../source/smart.element.js"></script>
     <script type="text/javascript" src="../../source/smart.button.js"></script>
     <script type="text/javascript" src="../../source/smart.fileupload.js"></script>
    </head>
    <body>
     <smart-file-upload></smart-file-upload>
    </body>
    </html>
    				

    Note how smart.element.js is declared before everything else. This is mandatory for all custom elements.

Create, Append, Remove, Get/Set Property, Invoke Method, Bind to Event


Create a new element:
const fileUpload = document.createElement('smart-file-upload');

Append it to the DOM:
document.body.appendChild(fileUpload);

Remove it from the DOM:
component.parentNode.removeChild(fileUpload);

Set a property:
component.propertyName = propertyValue;

Get a property value:
const propertyValue = fileUpload.propertyName;

Invoke a method:
fileUpload.methodName(argument1, argument2);

Add Event Listener:
const eventHandler = (event) => {
   // your code here.
};

fileUpload.addEventListener(eventName, eventHandler);

Remove Event Listener:
fileUpload.removeEventListener(eventName, eventHandler, true);

Appearance

File upload's structure contains browse button, file list(with all selected files), footer(with common buttons) and optional drop zone.
When an element is empty, it contains only 'browse' button. When user browses file/files, they are displayed in list. Below that list, is shown footer, contained following buttons - "Upload All", "Cancel All", "Pause All".

uploadUrl property defines path to the server that will handle the files, sent from the element. If this property is not set, as destination is used current location.

Demo

 <smart-file-upload upload-url="upload.php"></smart-file-upload>

Each file list item is visualized in container, included file name and buttons(Upload File, Cancel File, Pause File ).
Both "Upload File" and "Cancel File" buttons are shown permanently. "Pause File" is shown during the proces of upload to the server.

Demo

Setting "showProgress" to true includes additional progressbar at the bottom of the item's container.

Demo

User can upload files by dropping them in zone, defined by the value of "dropZone" property.

If 'dropZone' is set to true - the drop area is displayed inside the element.

Demo

 <smart-file-upload multiple drop-zone></smart-file-upload>

If "dropZone" is set to custom string(id of an element in the same page) - the drop area is append to the element with this .

Demo

 <div id="dropZoneCustomContainer"></div>
 <smart-file-upload multiple drop-zone="dropZoneCustomContainer"></smart-file-upload>

The file list also can be moved outside of the element by setting "appendTo" property to particular element's id.

Demo

 <smart-file-upload multiple append-to="fileListCustomContainer"></smart-file-upload>
 <div id="fileListCustomContainer"></div>

Behavior

By default File Upload allows single file.

Demo

Setting "multiple" to true allows upload of multiple files at the same time.

Demo

 <smart-file-upload multiple></smart-file-upload>

Setting "directory" to true allows upload of a directory. This feature works in part of the browsers(Google Chrome and Firefox for example)

Demo

 <smart-file-upload directory></smart-file-upload>

The element allows auto upload mode, when "autoUpload" is set to true. In this case file are uploaded immediatelly after their selection.

Demo

 <smart-file-upload auto-upload></smart-file-upload>

smartFileUpload's files can be validated via "validateFile" callback. As parameter is used an object, represented uploaded file. If the validation fails is fired "validationError" and file is not added to the file list.

Demo

<!DOCTYPE html>
<html lang="en">
<head>
 <link rel="stylesheet" href="../../source/styles/smart.default.css" type="text/css" />
 <script type="text/javascript" src="../../source/smart.element.js"></script>
 <script type="text/javascript" src="../../source/smart.button.js"></script>
 <script type="text/javascript" src="../../source/smart.fileupload.js"></script>
 <script>
        window.onload = function () {
            const fileUpload = document.querySelector('smart-file-upload');
            fileUpload.validateFile = function (file) {
                if (file.size > 204800) {
                    return false;
                }
                return true;
            };
        }
 </script>
</head>
<body>
 <smart-file-upload></smart-file-upload>
</body>
</html>

During process of upload the server response can vary. User can handle various cases via responseHandler

Demo

<!DOCTYPE html>
<html lang="en">
<head>
 <link rel="stylesheet" href="../../source/styles/smart.default.css" type="text/css" />
 <script type="text/javascript" src="../../source/smart.element.js"></script>
 <script type="text/javascript" src="../../source/smart.button.js"></script>
 <script type="text/javascript" src="../../source/smart.fileupload.js"></script>
 <script>
        window.onload = function () {
            const fileUpload = document.querySelector('smart-file-upload');
            fileUpload.responseHandler = function (xhr) {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    console.log(xhr.responseText);
                }
            };
        }
 </script>
</head>
<body>
 <smart-file-upload></smart-file-upload>
</body>
</html>

Methods

The element offers the following methods:

  • browse - browses for a file.
  • cancelAll - cancels all selected files. Files are removed from the file list and their upload is prevented.
  • cancelFile - cancels a file with particular file index. File is removed from the file list and it's upload is prevented.
  • pauseAll - pauses upload of all files. Files upload is prevented but files are ramaining in the file list.
  • pauseFile - pauses upload of a file with particular file index. File upload is prevented but file ramains in the file list.
  • uploadAll - uploads all selected files.
  • uploadFile - uploads file by given file's index.

Events

The element fires the following events:

  • fileSelected - triggered when a file has been selected.
  • uploadCanceled - triggered when a file upload operation is canceled.
  • uploadCompleted - triggered when a file upload operation is completed succesfully.
  • uploadError - triggered when during the file upload process something happens and upload fails.
  • uploadPaused - triggered when a file upload operation is paused.
  • uploadStarted - triggered when a file upload operation is started.
  • validationError - triggered if the validation of a user defined 'validateFile' callback fails.