使用RichTextEditor在Asp .Net Core中读取和更新txt.file

问题描述

我想读取一个静态txt文件,进行编辑,然后将其再次保存为asp .net Core MVC Web应用程序中的txt文件

我想知道是否可以通过使用可用的富文本编辑器之一(例如TinyMCE,CKEditor或quill)来实现这一目标。

下面是我当前拥有的代码

控制器

 public IActionResult Index()
        {
           
            return View(new Script());
        }

        [HttpPost]
        public ActionResult Index(Script script)
        {
            return View(script);
        }
 private void btnRead_Click(object sender,EventArgs e)
        {
            if (File.Exists(fileLoc))
            {
                using (TextReader tr = new StreamReader(fileLoc))
                {
                    MessageBox.Show(tr.ReadLine());
                }
            }
        }

课程

 public class Script
    {
        public int id { get; set; }
   
        [AllowHtml]
        public string code { get; set; }
    }

查看

@model Models.Script

@{
    ViewData["Title"] = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<body>

    @using (Html.BeginForm("Index","Scripts",FormMethod.Post))
    {
            @Html.LabelFor(m => m.code)<br/>
            @Html.TextAreaFor(m => m.code)
            @Html.ValidationMessageFor(m=>m.code,"",new { @class = "error" })
            <br/><br/>
            <input type="submit" value="Submit" />
            <span>@Html.Raw(Model.code)</span>
    }
</body>

@section Scripts{
    <script src="https://cdn.tiny.cloud/1/7wyujejhpvi3ixga3ss1q5gqwvocaiozzy83w2gvwcd004s4/tinymce/5/tinymce.min.js" referrerpolicy="origin"></script>
    <script>
        tinymce.init({
            selector: 'textarea',plugins: 'a11ychecker advcode casechange formatpainter linkchecker autolink lists checklist media mediaembed pageembed permanentpen powerpaste table advtable tinycomments tinymcespellchecker',toolbar: 'a11ycheck addcomment showcomments casechange checklist code formatpainter pageembed permanentpen table',toolbar_mode: 'floating',tinycomments_mode: 'embedded',tinycomments_author: 'Author name',});
    </script>
}

解决方法

以下是有关如何读取和上传txt文件的演示:

首先,在本地计算机上创建一个txt文件。

查看:

@model Models.Script

@{
    ViewData["Title"] = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<body>
    <input type="file" value="import"/>
    @using (Html.BeginForm("Index","Home",FormMethod.Post))
    {
            @Html.LabelFor(m => m.code)<br/>
            @Html.TextAreaFor(m => m.code)
            @Html.ValidationMessageFor(m=>m.code,"",new { @class = "error" })
            <br/><br/>
            <input type="submit" value="Submit" />
            <span>@Html.Raw(Model.code)</span>
    }
</body>

@section Scripts{
    <script src="https://cdn.tiny.cloud/1/7wyujejhpvi3ixga3ss1q5gqwvocaiozzy83w2gvwcd004s4/tinymce/5/tinymce.min.js" referrerpolicy="origin"></script>
    <script>
        tinymce.init({
            selector: 'textarea',plugins: 'a11ychecker advcode casechange formatpainter linkchecker autolink lists checklist media mediaembed pageembed permanentpen powerpaste table advtable tinycomments tinymcespellchecker',toolbar: 'a11ycheck addcomment showcomments casechange checklist code formatpainter pageembed permanentpen table',toolbar_mode: 'floating',tinycomments_mode: 'embedded',tinycomments_author: 'Author name',});
        $("input").change(function () {
            var fr = new FileReader();
            fr.onload = function () {
                tinyMCE.activeEditor.setContent(fr.result);
            }
            fr.readAsText(this.files[0]); 
        });         
    </script>
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new Script()
        {
            id = 1,code = "<h1>hello</h1>"
        };
        return View(model);
    }
    [HttpPost]
    public ActionResult Index(Script script)
    {
        StreamWriter sw = new StreamWriter("C:\\xxx\\Sample.txt");  
        //Write a line of text
        sw.WriteLine(script.code);
        //Close the file
        sw.Close();
        return View(script);
    }
}

结果: enter image description here