This example shows how to display documents in File Manager and open them in a popup dialog. The dialog opens Word documents (DOCX, RTF), Excel files (XLSX), Diagram data (JSON) and images.
Files to look at:
•
Index.cshtml
•
RichEditPartial.cshtml
•
SpreadsheetPartial.cshtml
•
DiagramPartial.cshtml
•
FileManagerApiController.cs
•
HomeController.cs
•
_Layout.cshtml
Configure ASP.NET Core project
If you start a new project, add required DevExpress libraries to your project as described in these tutorials:
Devextreme-Based Controls - Configure a non Visual Studio Project
Office-Inspired Controls - Configure a Visual Studio Project
Note This project targets .NET Core 3.0. To run the project in Visual Studio 2017, change the target framework in the project settings.
Configure FileManager and Popup
1. Add FileManager to your View. Connect FileManager to your file system like in the
Physical File System demo.
2. Create a dialog with the
Popup component:
[C#]
@(Html.DevExtreme().Popup().ID("dialogPopup"))
1. The dialog should display different content based on file type. To achieve this functionality, use the approach from the
Switching Templates On the Fly article and create
NamedTemplate for each control that opens files:
[C#]
@using(Html.DevExtreme().NamedTemplate("text")){}...
1. Handle the
FileManager.OnSelectedFileOpened event. Show your dialog and open selected fileItem in this event handler:
[JavaScript]
function onSelectedFileOpened(args){
openFileInDialog(args.fileItem);}
Add controls for different file types
This section describes how to open the most popular file types in different controls. It's not required to implement all of them.
• Rich Text Editor
• Spreadsheet
• Diagram
• Image
Rich Text Editor
1. The RichEdit component may have a lot of configuration options, so it's better to create it in a separate Partial View:
RichEditPartial.cshtml.
2. Render this Partial View inside a corresponding NamedTemplate:
[C#]
@using(Html.DevExtreme().NamedTemplate("text")){awaitHtml.RenderPartialAsync("RichEditPartial");}
1. Use RichEdit's
openDocument method to open the selected file content. For this, convert the ArrayBuffer object returned by
getItemContent to the base64 string.
[JavaScript]
FileLoader.loadRichEdit = function loadRichEdit(richEditControl, fileManager, fileItem, documentFormat){
fileManager.option("fileProvider").getItemContent([fileItem]).done(function(arrayBuffer){var base64Content = _fromArrayBufferToBase64(arrayBuffer);
richEditControl.openDocument(base64Content, fileItem.name, documentFormat);});};
back to the top
Spreadsheet
1. Add
Spreadsheet to a separate Partial View:
SpreadsheetPartial.cshtml.
2. Render the Partial View inside NamedTemplate:
[C#]
@using(Html.DevExtreme().NamedTemplate("excel")){ViewContext.Writer.Write("<div id='excelContainer'>");awaitHtml.RenderPartialAsync("SpreadsheetPartial");ViewContext.Writer.Write("</div>");}
1. Spreadsheet can open files only on the server side. Thus, to open a file, it's necessary to trigger a request to the server and pass the selected item key as the request parameter. When the request is completed, update Spreadsheet's container element:
[JavaScript]
FileLoader.loadSpreadsheet = function loadSpreadsheet(spreadsheetSelector, url, fileItem){
$.post(url, { filePath: fileItem.key }, function(data){ $(spreadsheetSelector).html(data);});};
1. In Controller, get a corresponding Excel document from the file system and return it as Model:
[C#]
publicIActionResultOpenDocInSpreadsheet(stringfilePath){returnPartialView("SpreadsheetPartial",GetDocumentModel(filePath));}
1. Open this object in the control with the
Spreadsheet.Open method:
[C#]
@(Html.DevExpress().Spreadsheet("spreadsheet").Open(Model?.DocumentID,DevExpress.Spreadsheet.DocumentFormat.Xlsx,()=>{returnModel?.FileBytes;}))
back to the top
Diagram
1. Create a separate Partial View with your Diagram:
DiagramPartial.cshtml.
2. Render this Partial View inside NamedTemplate:
[C#]
@using(Html.DevExtreme().NamedTemplate("diagram")){awaitHtml.RenderPartialAsync("DiagramPartial");}
1. Use the
fileProvider.getItemContent method to get the selected file content and open it in Diagram using the
import method:
[JavaScript]
FileLoader.loadDiagram = function loadDiagram(diagramSelector, fileManager, fileItem){
fileManager.option("fileProvider").getItemContent([fileItem]).done(function(arrayBuffer){var enc = new TextDecoder("utf-8");var data = enc.decode(arrayBuffer);
$(diagramSelector).dxDiagram("instance").import(data);});};
back to the top
Image
1. Add the IMG tag to the corresponding NamedTemplate:
[C#]
@using(Html.DevExtreme().NamedTemplate("image")){<imgid="imageViewer"/>}
1. Get the image content with the
fileProvider.getItemContent method. Convert the result to the base64 string and set the "src" attribute value to this string:
[JavaScript]
FileLoader.loadImage = function loadImage(imageSelector, fileManager, fileItem){
fileManager.option("fileProvider").getItemContent([fileItem]).done(function(arrayBuffer){var base64Content = _fromArrayBufferToBase64(arrayBuffer);
$(imageSelector).attr("src","data:image/jpg;base64," + base64Content);});};
back to the top