Getting Started with ASP.NET MVC File Manager Control
21 Jul 202615 minutes to read
This section briefly explains about how to include ASP.NET MVC File Manager control in your ASP.NET MVC application using Visual Studio.
Create an ASP.NET MVC Web App with HTML Helper
Create an ASP.NET MVC Web App using Visual Studio via Microsoft Templates or the Syncfusion® ASP.NET MVC Extension. For detailed instructions, refer to the ASP.NET MVC Getting Started documentation.
Install the required ASP.NET MVC package
To add ASP.NET MVC File Manager control in the app, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search for and install the Syncfusion.AspNetMvc.FileManager package. All Syncfusion ASP.NET MVC packages are available in nuget.org. See the NuGet packages topic for details.
Alternatively, you can install the same package using the Package Manager Console with the following command.
Install-Package Syncfusion.AspNetMvc.FileManager -Version 34.1.29Add the Namespace
After the package is installed, open the ~/Views/Web.config file and import the Syncfusion.EJ2 namespace.
<namespaces>
<add namespace="Syncfusion.EJ2" />
</namespaces>Add stylesheet and script resources
The theme stylesheet and script can be referenced from the CDN. Include the stylesheet and script references inside the <head> of ~/Views/Shared/_Layout.cshtml.
<head>
...
<!-- ASP.NET MVC controls styles -->
<link rel="stylesheet" href="https://cdn.syncfusion.com/ej2/34.1.29/fluent2.css" />
<!-- ASP.NET MVC controls scripts -->
<script src="https://cdn.syncfusion.com/ej2/34.1.29/dist/ej2.min.js"></script>
</head>Register the script manager
Open the ~/Views/Shared/_Layout.cshtml file and register the script manager EJS().ScriptManager() at the end of the <body> element as follows.
<body>
...
<!-- ASP.NET MVC Script Manager -->
@Html.EJS().ScriptManager()
</body>Add ASP.NET MVC File Manager Control
Add the ASP.NET MVC File Manager control in the ~/Views/Home/Index.cshtml file.
Update a ~/Controllers/HomeController.cs file with the following code. Create a Content/Files folder to store the files for the File Manager access. Make sure Microsoft.AspNet.Mvc is updated to the latest version.
<div class="control-section">
<div class="sample-container">
<!-- Filemanager control declaration -->
@Html.EJS().FileManager("file").AjaxSettings(new Syncfusion.EJ2.FileManager.FileManagerAjaxSettings
{
Url = "/Home/FileOperations"
}).NavigationPaneSettings(ls => ls.Visible(false)).ContextMenuSettings(ls => ls.Visible(false)).ToolbarSettings(ls => ls.Visible(false)).Render()
<!-- end of File Manager control -->
</div>
</div>using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Hosting;
using Newtonsoft.Json;
using Syncfusion.EJ2.FileManager.PhysicalFileProvider;
using Syncfusion.EJ2.FileManager.Base;
using System.Web;
using System;
using System.IO;
// Replace `FileManagerProject` with your project name.
namespace FileManagerProject.Controllers
{
public class HomeController : Controller
{
// Accessing the File Operations from File Manager package
PhysicalFileProvider operation = new PhysicalFileProvider();
public string basePath;
public HomeController()
{
this.basePath = HostingEnvironment.MapPath("~/Content/Files");
this.operation.RootFolder(HostingEnvironment.MapPath("~/Content/Files"));
}
public ActionResult FileOperations(FileManagerDirectoryContent args)
{
switch (args.Action)
{
case "read":
return Json(operation.ToCamelCase(operation.GetFiles(args.Path, args.ShowHiddenItems)));
case "delete":
return Json(operation.ToCamelCase(operation.Delete(args.Path, args.Names)));
case "details":
if(args.Names == null)
{
args.Names = new string[] { };
}
return Json(operation.ToCamelCase(operation.Details(args.Path, args.Names, args.Data)));
case "create":
return Json(operation.ToCamelCase(operation.Create(args.Path, args.Name)));
case "search":
return Json(operation.ToCamelCase(operation.Search(args.Path, args.SearchString, args.ShowHiddenItems, args.CaseSensitive)));
case "copy":
return Json(operation.ToCamelCase(operation.Copy(args.Path, args.TargetPath, args.Names, args.RenameFiles, args.TargetData)));
case "move":
return Json(operation.ToCamelCase(operation.Move(args.Path, args.TargetPath, args.Names, args.RenameFiles, args.TargetData)));
case "rename":
return Json(operation.ToCamelCase(operation.Rename(args.Path, args.Name, args.NewName)));
}
return null;
}
public ActionResult Upload(string path, IList<System.Web.HttpPostedFileBase> uploadFiles, string action)
{
try
{
FileManagerResponse uploadResponse;
foreach (var file in uploadFiles)
{
var folders = (file.FileName).Split('/');
// checking the folder upload
if (folders.Length > 1)
{
for (var i = 0; i < folders.Length - 1; i++)
{
string newDirectoryPath = Path.Combine(this.basePath + path, folders[i]);
if (Path.GetFullPath(newDirectoryPath) != (Path.GetDirectoryName(newDirectoryPath) + Path.DirectorySeparatorChar + folders[i]))
{
throw new UnauthorizedAccessException("Access denied for Directory-traversal");
}
if (!Directory.Exists(newDirectoryPath))
{
this.operation.ToCamelCase(this.operation.Create(path, folders[i]));
}
path += folders[i] + "/";
}
}
}
// 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("");
}
catch (Exception e)
{
ErrorDetails er = new ErrorDetails();
er.Message = e.Message.ToString();
er.Code = "417";
er.Message = "Access denied for Directory-traversal";
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.StatusCode = Convert.ToInt32(er.Code);
return Content("");
}
}
public ActionResult Download(string downloadInput)
{
FileManagerDirectoryContent args = JsonConvert.DeserializeObject<FileManagerDirectoryContent>(downloadInput);
return operation.Download(args.Path, args.Names);
}
public ActionResult GetImage(FileManagerDirectoryContent args)
{
return operation.GetImage(args.Path, args.Id, false, null, null);
}
public ActionResult Index()
{
return View();
}
}
}Run the application
Press Ctrl+F5 (Windows) or ⌘+F5 (macOS) to launch the application. The ASP.NET MVC File Manager control will render in your default web browser.

NOTE
The File Manager can be rendered with
local servicefor sending ajax request. Ajax request will be sent to the server which then processes the request and sends back the response. Refer Controller file for File Manager service.