Search results

Nested FileManager in ASP.NET Core FileManager control

FileManager can be rendered inside the other components like Tab, Dialog, and more.

Adding file manager inside the dialog

The following example shows the file manager component rendered inside the dialog. Click the browse button in the Uploader element to open the File Manager inside the Dialog control.

tagHelper
@{
    var defaultanimation = new Syncfusion.EJ2.Popups.DialogAnimationSettings { Effect = Syncfusion.EJ2.Popups.DialogEffect.None };
}

<div class=" control-section">
    <div class="sample-container">
        <div id='container' class="fileupload">
            <ejs-uploader id="UploadFile"> </ejs-uploader>
            <ejs-button id="openBtn" content="Browse..."></ejs-button>
        </div>
        <div id='target' class="control-section">
            <ejs-dialog id="dialog" animationSettings="defaultanimation" width="850px" target="#target" visible="false" open="dialogOpen" close="dialogClose" showCloseIcon="true" header="Open">
                <e-content-template>
                    <ejs-filemanager id="filemanager" fileOpen="onFileOpen" allowMultiSelection="false">
                        <e-filemanager-ajaxsettings  url="/Home/FileOperations"
                                                    downloadUrl="/Home/Download"
                                                    uploadUrl="/Home/Upload"
                                                    getImageUrl="/Home/GetImage">
                        </e-filemanager-ajaxsettings>
                    </ejs-filemanager>
                </e-content-template>
            </ejs-dialog>
        </div>
    </div>
</div>
<script>
    document.getElementById("openBtn").addEventListener('click', function () {
       var dialogObj = document.getElementById('dialog').ej2_instances[0];
       dialogObj.show();
       var filemanagerObj = document.getElementById('filemanager').ej2_instances[0];
       filemanagerObj.path = "/";
       filemanagerObj.refresh();
    });
    // Uploader will be shown, if Dialog is closed
    function dialogClose() {
        document.getElementById("container").style.display = "block";
    }
    // Uploader will be hidden, if Dialog is opened
    function dialogOpen() {
        document.getElementById("container").style.display = "none";
    }
    // File Manager's fileOpen event function
    function onFileOpen(args) {
        var file = args.fileDetails;
        if (file.isFile) {
            args.cancel = true;
            var uploadObject = document.getElementById('UploadFile').ej2_instances[0];
            var dialogObj = document.getElementById('dialog').ej2_instances[0];
            uploadObject.files = [{ name: file.name, size: file.size, type: file.type }];
            dialogObj.hide();
        }
    }

</script>

<style>

    .fileupload {
        max-width: 500px;
        margin: auto;
    }

    #openBtn {
        position: absolute;
        top: 67px;
        margin-left: 13px;
    }

    #target {
        height: 550px;
    }

    #dialog {
        top: 20px !important;
        max-height: 500px !important;
    }
</style>

Initially output be like the below.

FileManager upload

After clicking the file browser button, file manager is rendered with dialog. Output be like the below.

FileManager inside dialog

Adding file manager inside the tab

The following example demonstrate that the file manager component is placed inside the content area of tab element.

tagHelper
HomeController_core.cs
HomeController_mvc.cs
@{
    var contentOne = "#overview";
    var contentTwo = "#filemanager";
}

<div class="e-tab-section">
    <div class="col-lg-8 content-wrapper control-section">
        <div class="e-sample-resize-container">
            <div id="overview" style="display: none">
                <div class="content-title">
                    <div class="cnt-text">Overview</div>
                </div>
                <div>
                    The file manager component contains a context menu for performing file operations, large-icons view for displaying the files and folders, and a breadcrumb for navigation. However, these basic functionalities can be extended by using the additional feature modules like toolbar, navigation pane, and details view to simplify the navigation and file operations within the file system.
                </div>
            </div>
            <div id="filemanager" style="display: none">
                <div class="content-title">
                    <div class="cnt-text">Filemanager with Default Functionalities</div>
                </div>
                <ejs-filemanager id="file">
                    <e-filemanager-ajaxsettings url="/Home/FileOperations"
                                                downloadUrl="/Home/Download"
                                                uploadUrl="/Home/Upload"
                                                getImageUrl="/Home/GetImage">
                    </e-filemanager-ajaxsettings>
                </ejs-filemanager>
            </div>
            <ejs-tab id="orientationTab" height="320" heightAdjustMode="None" showCloseButton="true" selected="onSelect">
                <e-tab-tabitems>
                    <e-tab-tabitem header="ViewBag.headerText1" content="@contentOne"></e-tab-tabitem>
                    <e-tab-tabitem header="ViewBag.headerText2" content="@contentTwo"></e-tab-tabitem>
                </e-tab-tabitems>
            </ejs-tab>
        </div>
    </div>
</div>
<script>
    function onSelect() {
        var fileObj = document.getElementById("file").ej2_instances[0];
        fileObj.refreshLayout();
    }
</script>
<style>
    .e-content .e-item {
        font-size: 12px;
        padding: 10px;
        text-align: justify;
    }

    .content-title {
        height: 50px;
        display: table;
        margin: 0 auto;
    }

    .cnt-text {
        vertical-align: middle;
        display: table-cell;
        font-size: 18px;
        font-weight: 600;
    }
</style>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Syncfusion.EJ2.Navigations;
//File Manager's base functions are available in the below package
using Syncfusion.EJ2.FileManager.Base;
//File Manager's operations are available in the below package
using Syncfusion.EJ2.FileManager.PhysicalFileProvider;
using Newtonsoft.Json;


namespace WebApplication.Controllers
{
   public class HomeController : Controller
    {
        public PhysicalFileProvider operation;
        public string basePath;
        // Root Path in which files and folders are available.
        string root = "wwwroot\\Files";
        public HomeController(IHostingEnvironment hostingEnvironment)
        {
            // Map the path of the files to be accessed with the host
            this.basePath = hostingEnvironment.ContentRootPath;
            this.operation = new PhysicalFileProvider();
            // Assign the mapped path as root folder
            this.operation.RootFolder(this.basePath + "\\" + this.root);
        }

        public object FileOperations([FromBody] FileManagerDirectoryContent args)
        {
            // Restricting modification of the root folder
            if (args.Action == "delete" || args.Action == "rename")
            {
                if ((args.TargetPath == null) && (args.Path == ""))
                {
                    FileManagerResponse response = new FileManagerResponse();
                    ErrorDetails er = new ErrorDetails
                    {
                        Code = "401",
                        Message = "Restricted to modify the root folder."
                    };
                    response.Error = er;
                    return this.operation.ToCamelCase(response);
                }
            }
            // Processing the File Manager operations
            switch (args.Action)
            {
                case "read":
                    // Path - Current path; ShowHiddenItems - Boolean value to show/hide hidden items
                    return this.operation.ToCamelCase(this.operation.GetFiles(args.Path, args.ShowHiddenItems));
                case "delete":
                    // Path - Current path where of the folder to be deleted; Names - Name of the files to be deleted
                    return this.operation.ToCamelCase(this.operation.Delete(args.Path, args.Names));
                case "copy":
                    //  Path - Path from where the file was copied; TargetPath - Path where the file/folder is to be copied; RenameFiles - Files with same name in the copied location that is confirmed for renaming; TargetData - Data of the copied file
                    return this.operation.ToCamelCase(this.operation.Copy(args.Path, args.TargetPath, args.Names, args.RenameFiles, args.TargetData));
                case "move":
                    // Path - Path from where the file was cut; TargetPath - Path where the file/folder is to be moved; RenameFiles - Files with same name in the moved location that is confirmed for renaming; TargetData - Data of the moved file
                    return this.operation.ToCamelCase(this.operation.Move(args.Path, args.TargetPath, args.Names, args.RenameFiles, args.TargetData));
                case "details":
                    // Path - Current path where details of file/folder is requested; Name - Names of the requested folders
                    return this.operation.ToCamelCase(this.operation.Details(args.Path, args.Names));
                case "create":
                    // Path - Current path where the folder is to be created; Name - Name of the new folder
                    return this.operation.ToCamelCase(this.operation.Create(args.Path, args.Name));
                case "search":
                    // Path - Current path where the search is performed; SearchString - String typed in the searchbox; CaseSensitive - Boolean value which specifies whether the search must be casesensitive
                    return this.operation.ToCamelCase(this.operation.Search(args.Path, args.SearchString, args.ShowHiddenItems, args.CaseSensitive));
                case "rename":
                    // Path - Current path of the renamed file; Name - Old file name; NewName - New file name
                    return this.operation.ToCamelCase(this.operation.Rename(args.Path, args.Name, args.NewName));
            }
            return null;
        }

        
        
        // Processing the Upload operation
        public IActionResult Upload(string path, IList<IFormFile> uploadFiles, string action)
        {
            // Here we have restricted the upload operation for our online samples
            if (Response.HttpContext.Request.Host.Value == "ej2.syncfusion.com")
            {
                Response.Clear();
                Response.ContentType = "application/json; charset=utf-8";
                Response.StatusCode = 403;
                Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File Manager's upload functionality is restricted in the online demo. If you need to test upload functionality, please install Syncfusion Essential Studio on your machine and run the demo";
            }
            // Use below code for performing upload operation
            else
            {
                FileManagerResponse uploadResponse;
                //Invoking upload operation with the required paramaters
                // path - Current path where the file is to uploaded; uploadFiles - Files to be uploaded; action - name of the operation(upload)
                uploadResponse = operation.Upload(path, uploadFiles, action, null);
            }
            return Content("");
        }
        // Processing the Download operation
        public IActionResult Download(string downloadInput)
        {
            FileManagerDirectoryContent args = JsonConvert.DeserializeObject<FileManagerDirectoryContent>(downloadInput);
            //Invoking download operation with the required paramaters
            // path - Current path where the file is downloaded; Names - Files to be downloaded;
            return operation.Download(args.Path, args.Names);
        }
        // Processing the GetImage operation
        public IActionResult GetImage(FileManagerDirectoryContent args)
        {
            //Invoking GetImage operation with the required paramaters
            // path - Current path of the image file; Id - Image file id;
            return this.operation.GetImage(args.Path, args.Id, false, null, null);
        }
        public ActionResult Index()
        {
            ViewBag.headerText1 = new TabHeader { Text = "Overview" };
            ViewBag.headerText2 = new TabHeader { Text = "FileManager" };
            return View();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Syncfusion.EJ2.Navigations;
//File Manager's base functions are available in the below package
using Syncfusion.EJ2.FileManager.Base;
//File Manager's operations are available in the below package
using Syncfusion.EJ2.FileManager.PhysicalFileProvider;
using Newtonsoft.Json;
// Use the package for hosting
using System.Web.Hosting;


namespace WebApplication.Controllers
{
   public class HomeController : Controller
    {

        // Accessing the File Operations from File Manager package
        PhysicalFileProvider operation = new PhysicalFileProvider();
        public HomeController()
        {
            // Map the path of the files to be accessed with the host
            var path = HostingEnvironment.MapPath("~/Content/Files");
            // Assign the mapped path as root folder
            operation.RootFolder(path);
        }
        
        public ActionResult FileOperations(FileManagerDirectoryContent args)
        {
            // Processing the File Manager operations
            switch (args.Action)
            {
                case "read":
                    // Path - Current path; ShowHiddenItems - Boolean value to show/hide hidden items
                    return Json(operation.ToCamelCase(operation.GetFiles(args.Path, args.ShowHiddenItems)));
                case "delete":
                    // Path - Current path where of the folder to be deleted; Names - Name of the files to be deleted
                    return Json(operation.ToCamelCase(operation.Delete(args.Path, args.Names)));
                case "copy":
                    //  Path - Path from where the file was copied; TargetPath - Path where the file/folder is to be copied; RenameFiles - Files with same name in the copied location that is confirmed for renaming; TargetData - Data of the copied file
                    return Json(operation.ToCamelCase(operation.Copy(args.Path, args.TargetPath, args.Names, args.RenameFiles, args.TargetData)));
                case "move":
                    // Path - Path from where the file was cut; TargetPath - Path where the file/folder is to be moved; RenameFiles - Files with same name in the moved location that is confirmed for renaming; TargetData - Data of the moved file
                    return Json(operation.ToCamelCase(operation.Move(args.Path, args.TargetPath, args.Names, args.RenameFiles, args.TargetData)));
                case "details":
                    if (args.Names == null)
                    {
                        args.Names = new string[] { };
                    }
                    // Path - Current path where details of file/folder is requested; Name - Names of the requested folders
                    return Json(operation.ToCamelCase(operation.Details(args.Path, args.Names)));
                case "create":
                    // Path - Current path where the folder is to be created; Name - Name of the new folder
                    return Json(operation.ToCamelCase(operation.Create(args.Path, args.Name)));
                case "search":
                    // Path - Current path where the search is performed; SearchString - String typed in the searchbox; CaseSensitive - Boolean value which specifies whether the search must be casesensitive
                    return Json(operation.ToCamelCase(operation.Search(args.Path, args.SearchString, args.ShowHiddenItems, args.CaseSensitive)));
                case "rename":
                    // Path - Current path of the renamed file; Name - Old file name; NewName - New file name
                    return Json(operation.ToCamelCase(operation.Rename(args.Path, args.Name, args.NewName)));
            }
            return null;
        }
        
        // Processing the Upload operation
        public ActionResult Upload(string path, IList<System.Web.HttpPostedFileBase> uploadFiles, string action)
        {
            FileManagerResponse uploadResponse;
            //Invoking upload operation with the required paramaters
            // path - Current path where the file is to uploaded; uploadFiles - Files to be uploaded; action - name of the operation(upload)
            uploadResponse = operation.Upload(path, uploadFiles, action, null);

            return Content("");
        }
        // Processing the Download operation
        public ActionResult Download(string downloadInput)
        {
            FileManagerDirectoryContent args = JsonConvert.DeserializeObject<FileManagerDirectoryContent>(downloadInput);
            //Invoking download operation with the required paramaters
            // path - Current path where the file is downloaded; Names - Files to be downloaded;
            return operation.Download(args.Path, args.Names);
        }
        // Processing the GetImage operation
        public ActionResult GetImage(FileManagerDirectoryContent args)
        {
            //Invoking GetImage operation with the required paramaters
            // path - Current path of the image file; Id - Image file id;
            return operation.GetImage(args.Path, args.Id, false, null, null);
        }
        public ActionResult Index()
        {
            List<TabTabItem> orientationItems = new List<TabTabItem>
            {
                new TabTabItem { Header = new TabHeader { Text = "Overview" }, Content = "#overview" },
                new TabTabItem { Header = new TabHeader { Text = "Filemanager" }, Content = "#filemanager" }
            };
            ViewBag.orientationItems = orientationItems;
            return View();
        }
    }
}

Output be like the below for initial view.

FileManager inside tab

Output be like the below, when file manager is placed inside the tab

FileManager inside tab