Category / Section
How to save RTE content in a file at server
1 min read
Save RichTextEditor content in a file in the server
RichTextEditor content can be passed from vView to controller through XMLHttpRequest pPost. Content will be sent to the corresponding method into the controller and this value can be saved in a text file or any other format using streamWriter. Please rRefer to the below following given code.
cshtml
@{ ViewBag.Title = "Home Page"; } @using Uploader.Controllers; @using Syncfusion.EJ2.DropDowns <div> <div>@Html.EJS().RichTextEditor("default").Value((string)ViewBag.value).Render()</div> <div> <button id="btn">save content</button></div> </div> <script> document.getElementById('btn').addEventListener('click', function () { var obj = document.getElementById('default').ej2_instances[0]; var value = JSON.stringify({ text: obj.value }); const Http = new XMLHttpRequest(); const url = '@Url.Action("Save")'; Http.open("POST", url); Http.setRequestHeader('Content-Type', 'application/json'); Http.send(value); }); </script>
C#
[System.Web.Http.HttpPost] public ActionResult Save([FromBody]string text) { string RootPath = Server.MapPath("~/data.txt"); StreamWriter wirteFile = new StreamWriter(RootPath, true); wirteFile.WriteLine(text); wirteFile.Close(); wirteFile.Dispose(); return View(); }