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
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.1. To run the project in Visual Studio 2017, change the target framework in the project settings.
- Add FileManager to your View. Connect FileManager to your file system like in the Physical File System demo.
- Create a dialog with the Popup component:
@(Html.DevExtreme().Popup()
.ID("dialogPopup"))
- 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:
@using (Html.DevExtreme().NamedTemplate("text")) {
//
}
...
- Handle the FileManager.OnSelectedFileOpened event. Show your dialog and open selected fileItem in this event handler:
function onSelectedFileOpened(args) {
openFileInDialog(args.file);
}
This section describes how to open the most popular file types in different controls. It's not required to implement all of them.
- The RichEdit component may have a lot of configuration options, so it's better to create it in a separate Partial View: RichEditPartial.cshtml.
- Render this Partial View inside a corresponding NamedTemplate:
@using (Html.DevExtreme().NamedTemplate("text")) {
await Html.RenderPartialAsync("RichEditPartial");
}
- Use RichEdit's openDocument method to open the selected file content. For this, convert the ArrayBuffer object returned by getItemsContent to the base64 string.
FileLoader.loadRichEdit = function loadRichEdit(richEditControl, fileManager, fileItem, documentFormat) {
fileManager.option("fileSystemProvider").getItemsContent([fileItem]).done(function (arrayBuffer) {
var base64Content = _fromArrayBufferToBase64(arrayBuffer);
richEditControl.openDocument(base64Content, fileItem.name, documentFormat);
});
};
- Add Spreadsheet to a separate Partial View: SpreadsheetPartial.cshtml.
- Render the Partial View inside NamedTemplate:
@using (Html.DevExtreme().NamedTemplate("excel")) {
ViewContext.Writer.Write("<div id='excelContainer'>");
await Html.RenderPartialAsync("SpreadsheetPartial");
ViewContext.Writer.Write("</div>");
}
- 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:
FileLoader.loadSpreadsheet = function loadSpreadsheet(spreadsheetSelector, url, fileItem) {
//spreadsheetSelector is parent div selector, i.e."#excelContainer"
$.post(url, { filePath: fileItem.key }, function (data) { $(spreadsheetSelector).html(data); });
};
- In Controller, get a corresponding Excel document from the file system and return it as Model:
public IActionResult OpenDocInSpreadsheet(string filePath) {
return PartialView("SpreadsheetPartial", GetDocumentModel(filePath));
}
- Open this object in the control with the Spreadsheet.Open method:
@(Html.DevExpress()
.Spreadsheet("spreadsheet")
.Open(Model?.DocumentID, DevExpress.Spreadsheet.DocumentFormat.Xlsx, () => { return Model?.FileBytes; }))
- Create a separate Partial View with your Diagram: DiagramPartial.cshtml.
- Render this Partial View inside NamedTemplate:
@using (Html.DevExtreme().NamedTemplate("diagram")) {
await Html.RenderPartialAsync("DiagramPartial");
}
- Use the fileSystemProvider.getItemsContent method to get the selected file content and open it in Diagram using the import method:
FileLoader.loadDiagram = function loadDiagram(diagramSelector, fileManager, fileItem) {
fileManager.option("fileSystemProvider").getItemsContent([fileItem]).done(function (arrayBuffer) {
var enc = new TextDecoder("utf-8");
var data = enc.decode(arrayBuffer);
$(diagramSelector).dxDiagram("instance").import(data);
});
};
- Add the IMG tag to the corresponding NamedTemplate:
@using (Html.DevExtreme().NamedTemplate("image")) {
<img id="imageViewer" />
}
- Get the image content with the fileSystemProvider.getItemsContent method. Convert the result to the base64 string and set the "src" attribute value to this string:
FileLoader.loadImage = function loadImage(imageSelector, fileManager, fileItem) {
fileManager.option("fileSystemProvider").getItemsContent([fileItem]).done(function (arrayBuffer) {
var base64Content = _fromArrayBufferToBase64(arrayBuffer);
$(imageSelector).attr("src","data:image/jpg;base64," + base64Content);
});
};