Example of Import from Word in ASP.NET MVC Rich Text Editor Control
This example illustrates how to efficiently import the Word document into the Rich Text Editor.
@* The editor's settings are in the view; the data they read (ViewData) is set in the controller action (see the controller tab). *@
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "importEditor",
Name = "Import",
Height = "400px",
ToolbarSettings = new UiRichTextToolbar
{
Items = (List<string>)ViewData["Tools"]
},
ImportWord = new UiRichTextImportSettings { ServiceUrl = Url.Action("ImportWord", "RichTextEditorDemo") },
Value = (string)ViewData["RTEValue"]
})
@Html.UiRichTextEditor(new UiRichTextEditorOptions
{
Id = "importEditor",
Name = "Import",
Height = "400px",
ToolbarSettings = new UiRichTextToolbar
{
Items = (List<string>)ViewData["Tools"]
},
ImportWord = new UiRichTextImportSettings { ServiceUrl = Url.Action("ImportWord", "RichTextEditorDemo") },
Value = (string)ViewData["RTEValue"]
})
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class RichTextEditorController : Controller
{
// The page is Views/RichTextEditor/ImportFromWord.cshtml; it draws what ViewData holds.
public ActionResult ImportFromWord()
{
ViewData["RTEValue"] = "<p>Put the caret where the document should go, then use <b>Import from Word</b>.</p><p><br></p>";
ViewData["Tools"] = new List<string> { "ImportWord", "|", "Bold", "Italic", "Underline", "|", "Formats", "|", "OrderedList", "UnorderedList", "|", "Undo", "Redo" };
return View();
}
/// <summary>
/// The Import sample's action (ImportWord.ServiceUrl): a .docx in (multipart, field "file"), { html } out, made safe; or 400 with
/// { error } for a file that is too large, damaged, not a .docx, or unsafe to open.
/// </summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult ImportWord(HttpPostedFileBase file)
{
string html, error;
if (file == null || file.ContentLength <= 0) { error = "No file was sent."; }
else if (UiRichTextWord.TryImport(file.InputStream, out html, out error))
{
return Json(new { html });
}
Response.StatusCode = 400;
Response.TrySkipIisCustomErrors = true;
return Json(new { error });
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
@* Rich Text Editors with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
This sample demonstrates the Import/Export feature of the Rich Text Editor, which allows users to import the Word document into the editor. The word document can be imported as Rich Text Editor content by clicking the import to Word icon, browsing for the document, and uploading it to the server, where it is converted to HTML and then sent to the Rich Text Editor as its value.