This example allows users to preview document content in a popup dialog. The dialog can display Word documents (DOCX, RTF), Excel files (XLSX), Diagram data (JSON), and images. Document selection is implemented using a File Manager.
If you start a new project, add required DevExpress libraries to your project as described in the following help topics:
- DevExtreme-Based Controls - Configure a non-Visual Studio Project
- DevExtreme-Based Controls - Configure a Visual Studio Project
- Office-Inspired Controls - Get Started with the Rich Text Editor
- Office-Inspired Controls - Get Started with the Spreadsheet
-
Add a FileManager to a View. Connect the FileManager to your file system as described in the following demo: Physical File System.
-
Create a dialog using a Popup component:
@(Html.DevExtreme().Popup() .ID("dialogPopup"))
-
Create a
NamedTemplatefor each file type users can preview. Refer to the following help topic for additional information: Switching Templates on the Fly.@using (Html.DevExtreme().NamedTemplate("text")) { // }
-
Handle the FileManager.OnSelectedFileOpened event to open your dialog and pass the selected file:
function onSelectedFileOpened(args) { openFileInDialog(args.file); }
In this example, the following controls display file content:
-
When using the RichEdit component, you may specify dozens of configuration options. In such instances, create a separate Partial View. Render the Partial View within a
NamedTemplate:@using (Html.DevExtreme().NamedTemplate("text")) { await Html.RenderPartialAsync("RichEditPartial"); }
-
Call the RichEdit.openDocument method to open the selected file:
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 a Spreadsheet control to a separate Partial View. Render the Partial View within a
NamedTemplate:@using (Html.DevExtreme().NamedTemplate("excel")) { ViewContext.Writer.Write("<div id='excelContainer'>"); await Html.RenderPartialAsync("SpreadsheetPartial", new FileManagerOpenDocuments.Models.DocumentModel()); ViewContext.Writer.Write("</div>"); }
-
Spreadsheet can open files only on the server. Trigger a request to the server and pass the selected item's key as the request parameter. When the request is completed, update the Spreadsheet 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); }); };
-
Obtain the Excel document from the file system in the Controller and return it as a model:
return PartialView("SpreadsheetPartial", new DocumentModel { DocumentID = relativePath, FileBytes = System.IO.File.ReadAllBytes(path) });
-
Call the Spreadsheet.Open method to open the Excel document:
@(Html.DevExpress() .Spreadsheet("spreadsheet") .Open(Model?.DocumentID, DevExpress.Spreadsheet.DocumentFormat.Xlsx, () => { return Model?.FileBytes; }))
-
Create a Partial View that contains a Diagram. Render the Partial View within a
NamedTemplate:@using (Html.DevExtreme().NamedTemplate("diagram")) { await Html.RenderPartialAsync("DiagramPartial"); }
-
Call the fileSystemProvider.getItemsContent method to obtain selected file content. Call the import method to load that content into the Diagram:
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 an
<img>tag to theNamedTemplate:@using (Html.DevExtreme().NamedTemplate("image")) { <img id="imageViewer" /> }
-
Call the fileSystemProvider.getItemsContent method to obtain image content. Convert the result to a
base64string and assign this string to thesrcattribute: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); }); };
- Index.cshtml
- RichEditPartial.cshtml
- SpreadsheetPartial.cshtml
- DiagramPartial.cshtml
- FileManagerApiController.cs
- HomeController.cs
- Program.cs
- _Layout.cshtml
(you will be redirected to DevExpress.com to submit your response)