Sort the selected files

21 Dec 20223 minutes to read

You can sort the selected files in uploader control by using the selected event. Refer to the following example.

<div class="control_wrapper">
    @Html.EJS().Uploader("UploadFiles").Selected("onSelect").AsyncSettings(new Syncfusion.EJ2.Inputs.UploaderAsyncSettings { SaveUrl = "https://services.syncfusion.com/aspnet/production/api/FileUploader/Save", RemoveUrl = "https://services.syncfusion.com/aspnet/production/api/FileUploader/Remove" }).Render()
</div>

<script>
    var initial = true
    function onSelect(args) {
        if (initial) { initial = false; return; }
        args.isModified = true;
        var uploadObj = document.getElementById("UploadFiles").ej2_instances[0];
        let oldFiles = uploadObj.getFilesData();
        let filesData = args.filesData.concat(oldFiles);
        let modifiedData = sortFileList(filesData);
        args.modifiedFilesData = modifiedData;
    }

    function sortFileList(filesData) {
        let files = filesData;
        let fileNames = [];
        for (let i = 0; i < files.length; i++) {
            fileNames.push(files[i].name);
        }
        let sortedFileNames = fileNames.sort();
        let sortedFilesData = [];
        let index = 0;
        for (let name of sortedFileNames) {
            for (let i = 0; i < files.length; i++) {
                if (name === files[i].name) {
                    sortedFilesData.push(files[i]);
                }
            }
        }
        return sortedFilesData;
    }
</script>
.control_wrapper {
    max-width: 400px;
    margin: 0 auto;
}

NOTE

You can also explore ASP.NET MVC File Upload feature tour page for its groundbreaking features. You can also explore our ASP.NET MVC File Upload example to understand how to browse the files which you want to upload to the server.