CyberStore Ecommerce 2023 Documentation
File_FileUploader Widget

A file upload widget. Introduced in 2023.1. 

 Remarks

The File_FileUploader widget creates a file uploader widget that can be added to any page. Widget options provide a way to customize functionality and widget Display. Widget options also allow the administrator to  add prefixes and suffixes to the name of the files that are uploaded.  

 

The image above shows two instances of the widget on the same page but customized through the widget options.

 

 Widget Option Usage

Widget options provide a way to customize widgets in various ways. Widget options are sent to the widget when loaded with the LoadWidgetControl.

allowMultipleFiles --  (true/false) will determine if the widget can upload 1 or multiple files.

allowedExtensionsForUpload  -- Sets The types of files that can be uploaded.

maxFileSize -- set the maximum file size

displayRules -- (true/false) the allowed extensions and the max file size will be displayed.

label -- The label for the widget is also customizable with the label option which will display any text entered by the user. The user can choose to create a box display where files can be dragged

allowDragAndDropZone  -- (True / Fales) create a box display where files can be uploaded from

strObscureFilename -- (True / Fales) saved file can be renamed to a randomly generated name

filePrefix -- A custome prefix can be added to the saved file name

 fileSuffix -- A custome suffix can be added to the saved file name

uploadMode --  ('instantly, useButton') files are uploaded "instantly" or when a button is clicked

 Widget Conventions
By default, the uploaded files are saved to a folder on your webserver's /Uploads folder and then a subfolder for each Customer (referencing the Customer Number).
This image below shows the same files uploaded by the two widgets shown above. The widget options for the two widgets are shown in the example below. The Customer Number is '000000000000010'
 Example

See an example of how to load and configure this widget in SitePages.config.

<!-- These are the widget options for the first widget -->
<Control src="LoadWidgetControl.ascx" FileLocation="File_FileUploader.html" Options="
    allowMultipleFiles: true,
    maxFileSize: 4500000,
    allowedExtensionsForUpload: '.jpg, .jpeg, .gif, .png, .pdf',
    uploadMode: 'instantly',
    allowDragAndDropZone: false,
    strObscureFilename: false,
    filePrefix: 'Prefix_One_',
    fileSuffix: '_Suffix_One',
    displayRules: true,
    label: 'File Uploader'" 
/>

<!-- These are the widget options for shen using multiple times on a page. This is the second widget -->
<Control src="LoadWidgetControl.ascx" FileLocation="File_FileUploader.html" Options="
    allowMultipleFiles: true,
    maxFileSize: 4500000,
    allowedExtensionsForUpload: '.jpg, .jpeg, .gif, .png, .pdf',
    uploadMode: 'instantly',
    allowDragAndDropZone: true,
    strObscureFilename: true,
    filePrefix: 'Prefix_Two',
    fileSuffix: '_Suffix_Two',
    displayRules: false,
    label: 'File Uploader'" 
/>
 Widget Source

The following is the source code for this widget.

Developer's Note:

To create a custom version of the widget, copy all of the code below into a file of the same name and place it into your Site's widgets folder (e.g., ../YourSiteFolder/Widgets). The CyberStore page engine will then override the default source with your customized version.

<div id='widgetOptions_widgetOptions' class='widget container'>
    <div>
        <h3 class='title_widgetOptions'>File UpLoader</h3>
    </div>
    <div>
        <div class='note_widgetOptions note'>Allowed file extensions: <div class='fileTypeDisplay_widgetOptions'></div></div>
        <div class='note_widgetOptions note'>Maximum file size: <div class='maxFileSizeDisplay_widgetOptions'></div></div>
    </div>
    <div id='fileUpLoaderDiv_widgetOptions'></div>

    <!-- drop zone html used if widgetOptions.allowDragAndDropZone == true -->
    <div id='dropzone-external_widgetOptions' class='flex-box dx-theme-border-color'>
        <div id='dropzone-text_widgetOptions' class='flex-box'>
            <span>Drop file here</span>
            <span>…or click to browse.</span>
        </div>
        <div id='upload-progress_widgetOptions'></div>
    </div>

</div>

<script type='text/javascript'>
    // '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    // ' Base Widget:  File_FileUploader.html
    // '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    // ' Custom Widget Filename:
    // '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    // ' (c) 2021 by Dovetail Internet Technologies, LLC
    // '              www.dovetailinternet.com
    // '              info@dovetailinternet.com
    // '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    // ' All rights reserved. Not to be used without permission.
    // '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    // ' Development Date: June 2021
    // ' Revision: 1.0
    // ' Modified: 06/10/2021 - PR
    // '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    // ' Purpose: Create a portable and versatile file upload widget that can be instantiated multiple times on the same page.
    // '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    var FileUploadList = [];

    $(function () {

        var allowedExtensionsForUploadArray = widgetOptions.allowedExtensionsForUpload.split(',');
        var allowedExtensions = buildFileTypesArray(allowedExtensionsForUploadArray);

        // update widget displays with widget info
        $('.maxFileSizeDisplay_widgetOptions').html((widgetOptions.maxFileSize / 1000000) + ' MB');
        $('.fileTypeDisplay_widgetOptions').html(buildDisplayString(allowedExtensions));
        $('.title_widgetOptions').html(widgetOptions.label);
        (widgetOptions.displayRules) ? $('note_widgetOptions').show() : $('.note_widgetOptions').hide();

        function buildDisplayString(someArray) {
            var html = '';
            someArray.forEach(function (item, i, someArray) {
                if (i % 5 == 0) {
                    if (i !== 0) { html += '</br>'; }
                }
                html += item + '  ';
            });
            return html;
        }

        function buildFileTypesArray(anArray) {
            var returnArray = [];
            if (anArray.length <= 0) {
                returnArray = ['.jpg', '.jpeg', '.gif', '.png', '.pdf']; // default file types
            }
            if (anArray.length > 0) {
                returnArray = anArray.map(item => item.replace(/\s/g, ''));
                returnArray = returnArray.map(function (item) {
                    if (item.startsWith('.')) {
                        return item;
                    }
                    if (!item.startsWith('.')) {
                        var newItem = '.' + item;
                        return newItem;
                    }
                })
            }
            return returnArray;
        }

        DisplayDropAndDragZone(widgetOptions.allowDragAndDropZone);

        function DisplayDropAndDragZone(allowDragAndDropZone) {
            // create class for widget
            var widgetClass = '.fileUpLoader_yesZone_widgetOptions';
            $('#fileUpLoaderDiv_widgetOptions').addClass('fileUpLoader_yesZone_widgetOptions');

            if (allowDragAndDropZone == true) {
                ///---------------- create widget with drag and Drop zone -----------------
                var fileUploader_yesZone = $(widgetClass).dxFileUploader({
                    dropZone: $(widgetOptions.dragAndDropZone),
                    invalidFileExtensionMessage: 'File type is not allowed',
                    labelText: 'or Drop file here',
                    uploadButtonText: 'Upload',
                    uploadUrl: FileUploadUtil(
                        widgetOptions.strObscureFilename,
                        widgetOptions.filePrefix,
                        widgetOptions.fileSuffix),
                    multiple: widgetOptions.allowMultipleFiles ?? true, // option
                    uploadMode: widgetOptions.uploadMode,
                    maxFileSize: widgetOptions.maxFileSize ?? 1500000,
                    dialogTrigger: '#dropzone-external_widgetOptions',
                    dropZone: '#dropzone-external_widgetOptions',
                    allowedFileExtensions: allowedExtensions,
                    visible: false,
                    onDropZoneEnter: function (e) {
                        if (e.dropZoneElement.id === 'dropzone-external_widgetOptions')
                            toggleDropZoneActive(e.dropZoneElement, true);
                    },
                    onDropZoneLeave: function (e) {
                        if (e.dropZoneElement.id === 'dropzone-external_widgetOptions')
                            toggleDropZoneActive(e.dropZoneElement, false);
                    },
                    onProgress: function (e) {
                        uploadProgressBar.option('value', e.bytesLoaded / e.bytesTotal * 100)
                    },
                    onUploadStarted: function () {
                        uploadProgressBar.option('visible', true);
                    },
                    onUploaded: function (e) {
                        var response = JSON.parse(e.request.responseText);
                        console.log('response');
                        console.log(response);
                        if (response.Result.Success) {
                            FileUploadList = response.Data.Files;
                            if (!widgetOptions.allowMultipleFiles) {
                                e.component.option('disabled', true); // sets the widget as disabled so you can't upload again
                            }
                        }
                        else {
                            e.component.reset();
                            FileUploadList = [];
                        }
                        var alertResult = DevExpress.ui.dialog.alert(response.Result.Message,
                            (response.Result.Success)
                                ? 'Message'
                                : 'Error'
                        );
                    }
                }).dxFileUploader('instance');
            }

            if (allowDragAndDropZone == false) {
                //----------- create widget without the external drag and Drop zone ----------------------
                // create class for widget
                var widgetClass = '.fileUpLoader_noZone_widgetOptions';
                $('#fileUpLoaderDiv_widgetOptions').addClass('fileUpLoader_noZone_widgetOptions');
                RemoveElement('dropzone-external_widgetOptions');
                var fileUploader = $(widgetClass).dxFileUploader({
                    dropZone: $(widgetOptions.dragAndDropZone),
                    invalidFileExtensionMessage: 'File type is not allowed',
                    labelText: 'or Drop file here',
                    uploadButtonText: 'Upload',
                    uploadUrl: FileUploadUtil(widgetOptions.strObscureFilename ?? false, widgetOptions.filePrefix ?? 'RMA', widgetOptions.fileSuffix ?? '_the End'),
                    multiple: widgetOptions.allowMultipleFiles ?? true, // option
                    uploadMode: widgetOptions.uploadMode,
                    maxFileSize: widgetOptions.maxFileSize ?? 1500000,
                    allowedFileExtensions: allowedExtensions,
                    onUploaded: function (e) {
                        console.log(e);
                        var response = JSON.parse(e.request.responseText);

                        if (response.Result.Success) {
                            FileUploadList = response.Data.Files;
                            if (!widgetOptions.allowMultipleFiles) {
                                e.component.option('disabled', true); // sets the widget as disabled so you can't upload again
                            }
                        }
                        else {
                            e.component.reset();
                            FileUploadList = [];
                        }
                        var alertResult = DevExpress.ui.dialog.alert(response.Result.Message,
                            (response.Result.Success)
                                ? 'Message'
                                : 'Error'
                        );
                    }
                }).dxFileUploader('instance');
            }
        }

        function toggleDropZoneActive(dropZone, isActive) {
            if (isActive) {
                dropZone.classList.add('dx-theme-accent-as-border-color');
                dropZone.classList.remove('dx-theme-border-color');
                dropZone.classList.add('dropzone-active');
            } else {
                dropZone.classList.remove('dx-theme-accent-as-border-color');
                dropZone.classList.add('dx-theme-border-color');
                dropZone.classList.remove('dropzone-active');
            }
        }
        var uploadProgressBar = $('#upload-progress_widgetOptions').dxProgressBar({
            min: 0,
            max: 100,
            width: '30%',
            showStatus: false,
            visible: false
        }).dxProgressBar('instance');
    });

</script>


<style>
    .note {
        display: block;
        font-size: 10pt;
        color: #484848;
        margin-left: 9px;
    }

        .note > span {
            font-weight: 700
        }

        .note > div {
            font-weight: 700
        }

    #dropzone-external_widgetOptions {
        width: 250px;
        height: 200px;
        background-color: rgba(183, 183, 183, 0.1);
        border-width: 2px;
        border-style: dashed;
        padding: 10px;
    }

        #dropzone-external_widgetOptions > * {
            pointer-events: none;
        }

        #dropzone-external_widgetOptions.dropzone-active {
            border-style: solid;
        }

    .widget-container > span {
        font-size: 22px;
        font-weight: bold;
        margin-bottom: 16px;
    }

    #dropzone-image_widgetOptions {
        max-width: 100;
        max-height;
    }

    #dropzone-external_widgetOptions_widgetOptions > span {
        font-weight: 100;
        opacity: 0.5;
    }

    #upload-progress_widgetOptions {
        display: flex;
        margin-top: 10px;
    }

    .flex-box {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
    }
</style>