Back to Devexpress

Document Management

aspnetcore-400972-rich-edit-document-management.md

latest5.4 KB
Original Source

Document Management

  • Sep 09, 2024
  • 3 minutes to read

Commands on the File ribbon tab can be used to manage documents.

The sections below provide information on how to invoke these commands from code.

Create a Document

Call the newDocument method to create a new document.

javascript
richEdit.newDocument();

Open a Document

Call the openDocument method to open a document.

  • Call the method with parameters to open a particular document.
javascript
var documentAsBase64 = "UEsDBAoAAAAAACF<...>DAAA8NQAAAAA="
richEdit.openDocument(documentAsBase64, "DocumentName.docx", DevExpress.RichEdit.DocumentFormat.OpenXml)
  • Call the method without parameters to invoke the Open dialog.
javascript
richEdit.openDocument();

Save a Document

Note

If the document is not saved in the client-side or server-side event handler, the Save button and a call to the saveDocument method have no effect.

When a user clicks the Save button or the saveDocument method is called, the Rich Text Editor converts the document’s content to base64 and invokes the Saving event. Write the event handler to save the document on the client side. Set the handled property to true to prevent further processing.

cshtml
@(Html.DevExpress().RichEdit("richEdit")
    ...
    .OnSaving(
        "function(s, e) {" +
        "e.handled = true;" +
        "console.log(e.base64);" +
        "console.log(e.fileName);" +
        "console.log(e.format);" +
        "}")
...

If the handled property is not set to true (the event is not handled), specify the ExportUrl(String) property to save the document on the server side.

cshtml
@(Html.DevExpress().RichEdit("richEdit")
   .ExportUrl(Url.Action("Export"))
   .Height(600)
   .ConfirmOnLosingChanges(c => c.Enabled(false))
)
csharp
public IActionResult Export(string base64, string fileName, DevExpress.AspNetCore.RichEdit.DocumentFormat format, string reason) {
    byte[] fileContents = System.Convert.FromBase64String(base64);
    return Ok();
}

View Example: How to load/save documents from/to a database

Note

When RichEdit sends a document to the server, the control encodes the document content in UTF-8. To get the file content in string format, use a converter.

csharp
byte[] fileContents = System.Convert.FromBase64String(base64);
string rtfText = Encoding.UTF8.GetString(fileContents);

Note

If you apply the [ApiController] attribute to your controller, you should add the [FromForm] attribute to parameters in the Export action.

csharp
public IActionResult Export([FromForm]string base64, [FromForm]string fileName, [FromForm]int format, [FromForm]string reason) {  
  byte[] fileContents = System.Convert.FromBase64String(base64);  
  return Ok();  
}

If document saving is implemented on the server-side, you can write the Saved event handler to perform custom actions after a server sends a response to a save request.

Learn how to use the ExportUrl method in a Razor Pages web app.

Autosave

You can implement the document autosave feature in the following way:

javascript
setInterval(function() {
    if (richEdit.hasUnsavedChanges)
        richEdit.saveDocument(DevExpress.RichEdit.DocumentFormat.OpenXml);
}, 30000);

Download a Document

Call the downloadDocument method to download a document.

javascript
//downloads the myDocument.txt file
richEdit.downloadDocument(DevExpress.RichEdit.DocumentFormat.PlainText, "myDocument");

To download the document in a portable document format (PDF), call the downloadPdf method.

See Also

Document Management - Online Demo