標籤:
最近看到很多網站後台都用到了富文本,包括自己所在的公司也是。公司用的KindEditor,所以就講講KindEditor。之前我也沒學過,所以網上搜了一篇博文,直接轉載如下(PS:完全以學習為目的哦~)
最近轉入Asp.net MVC開發,WebForm下慣用的FreeTextBox已然不能用了。於是想找個功能全、介面雅、免費的純JS編輯器,KindEditor4正好滿足本人的要求,包括圖片、Flash、視頻上傳及空間管理而且配置也異常的簡單。下面簡單說一下在Asp.net MVC環境下基本配置方法。
Step 1. 下載KindEditor
首先到KindEditor官網下載(目前是4.1.7版本),解壓後刪除jsp、php、asp、examples檔案夾,放入Asp.net MVC項目中的Scripts檔案夾中。
Step 2. 添加HomeController
public class HomeController : Controller { // // GET: /Home/ [ValidateInput(false)] public ActionResult Index() { return View(); } [ValidateInput(false)] [HttpPost] public ActionResult Index(string content) { ViewBag.Content = content; return View(); } }
注意ValidateInput特性設定為false,否則無法插入Html標記。
Step 3. 在視圖中加入KindEditor指令碼
<script src="../../Scripts/kindeditor/kindeditor-min.js" type="text/javascript"></script><script type="text/javascript"> KindEditor.ready(function (K) { var options = { uploadJson: ‘../scripts/kindeditor/asp.net/upload_json.ashx‘, fileManagerJson: ‘../scripts/kindeditor/asp.net/file_manager_json.ashx‘, allowFileManager : true }; window.editor = K.create(‘#content‘, options); });</script><h2>KindEditor4編輯器</h2>@Html.Raw(@ViewBag.Content)@using (Html.BeginForm()){ <textarea id="content" name="content" style="width:700px;height:300px;"></textarea> <input type="submit" value="提交" />}
- uploadJson和fileManagerJson設定值要注意路徑名稱
- window.editor = K.create(‘#content‘, options);中的#content要與textarea標記的id一致
- 顯示輸出時,使用Html.Raw輔助方法才能正確顯示Html
Step 4. 引用LitJSON.dll
項目引用KindEditor/asp.net/bin目錄下的LitJSON.dll。
現在已經可以運行了。如果想要修改上傳檔案大小的限制,必須修改upload_json.ashx程式中的maxSize以及修改項目的Web.Config,在<system.web>中加入諸如<httpRuntime maxRequestLength="20000000" executionTimeout="3600" />(此處限制上傳檔案20MB)。上傳的檔案放置在KindEditor/attached目錄下,如需修改,可分別在upload_json.ashx及file_manager_json.ashx中修改儲存路徑。
下載Demo
<script type="text/javascript"><!-- google_ad_client = "ca-pub-1944176156128447"; /* cnblogs 首頁橫幅 */ google_ad_slot = "5419468456"; google_ad_width = 728; google_ad_height = 90; //--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
轉載自:http://blog.csdn.net/dyllove98/article/details/9070125
富文本KidnEditor在MVC中的應用