Skip to content

About

Display documents in File Manager and open them in a popup dialog

Topics

Resources

Stars

4 stars

Watchers

54 watching

Forks

Latest commit

 

History

95 Commits

Folders and files

Repository files navigation

ASP.NET Core File Manager and Popup - Select Documents and Preview Content

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.

Configure an ASP.NET Core Project

If you start a new project, add required DevExpress libraries to your project as described in the following help topics:

Configure FileManager and Popup Controls

  1. Add a FileManager to a View. Connect the FileManager to your file system as described in the following demo: Physical File System.

  2. Create a dialog using a Popup component:

    @(Html.DevExtreme().Popup()
    	.ID("dialogPopup"))
  3. Create a NamedTemplate for 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")) {
    	//
    }
  4. Handle the FileManager.OnSelectedFileOpened event to open your dialog and pass the selected file:

    function onSelectedFileOpened(args) {
    	openFileInDialog(args.file);
    }

Add Controls that Display File Content

In this example, the following controls display file content:

Rich Text Editor

  1. 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");
    }
  2. 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);
    		});
    	};

Spreadsheet

  1. 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>");
    }
  2. 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);  });
    	};
  3. 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)
    });
  4. Call the Spreadsheet.Open method to open the Excel document:

    @(Html.DevExpress()
    .Spreadsheet("spreadsheet")
    .Open(Model?.DocumentID, DevExpress.Spreadsheet.DocumentFormat.Xlsx, () => { return Model?.FileBytes; }))

Diagram

  1. Create a Partial View that contains a Diagram. Render the Partial View within a NamedTemplate:

    @using (Html.DevExtreme().NamedTemplate("diagram")) {
    	await Html.RenderPartialAsync("DiagramPartial");
    }
  2. 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);
    		});
    	};

Image

  1. Add an <img> tag to the NamedTemplate:

    @using (Html.DevExtreme().NamedTemplate("image")) {
    	<img id="imageViewer" />
    }
  2. Call the fileSystemProvider.getItemsContent method to obtain image content. Convert the result to a base64 string and assign this string to the src attribute:

    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);
    		});
    	};

Files to Review

Does This Example Address Your Development Requirements/Objectives?

(you will be redirected to DevExpress.com to submit your response)

About

Display documents in File Manager and open them in a popup dialog

Topics

Resources

Stars

4 stars

Watchers

54 watching

Forks

Used by

Contributors

Languages