一步步教你在Asp.net Mvc中使用UEditor編輯器
最近在配置.net mvc4項(xiàng)目使用ueditor編輯器,在配置過程中遇見了好幾個(gè)問題,以此來記錄解決辦法。
首先去這里下載
然后重名一下下載的文件夾為ueditor(想自己起名字也可以,不過配置文件就要改成一樣的了,這里為了避免麻煩,就跟配置文件里的名字保持一致了)
然后根據(jù)自己需要放到項(xiàng)目中,我是這么放的
上面建的文件夾也是跟配置文件保持一致,并不是非要照著這么做,這里只是跟配置文件保持一致,可以根據(jù)自己需要修改配置文件,自己想怎么建都行,看看配置文件就明白了
config.json
/* 前后端通信相關(guān)的配置,注釋只允許使用多行方式 */ { /* 上傳圖片配置項(xiàng) */ "imageActionName": "uploadimage", /* 執(zhí)行上傳圖片的action名稱 */ "imageFieldName": "upfile", /* 提交的圖片表單名稱 */ "imageMaxSize": 2048000, /* 上傳大小限制,單位B */ "imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上傳圖片格式顯示 */ "imageCompressEnable": true, /* 是否壓縮圖片,默認(rèn)是true */ "imageCompressBorder": 1600, /* 圖片壓縮最長邊限制 */ "imageInsertAlign": "none", /* 插入的圖片浮動(dòng)方式 */ "imageUrlPrefix": "Scripts/ueditor/net/", /* 圖片訪問路徑前綴 */ "imagePathFormat": "upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳保存路徑,可以自定義保存路徑和文件名格式 */ /* {filename} 會(huì)替換成原文件名,配置這項(xiàng)需要注意中文亂碼問題 */ /* {rand:6} 會(huì)替換成隨機(jī)數(shù),后面的數(shù)字是隨機(jī)數(shù)的位數(shù) */ /* {time} 會(huì)替換成時(shí)間戳 */ /* {yyyy} 會(huì)替換成四位年份 */ /* {yy} 會(huì)替換成兩位年份 */ /* {mm} 會(huì)替換成兩位月份 */ /* {dd} 會(huì)替換成兩位日期 */ /* {hh} 會(huì)替換成兩位小時(shí) */ /* {ii} 會(huì)替換成兩位分鐘 */ /* {ss} 會(huì)替換成兩位秒 */ /* 非法字符 \ : * ? " < > | */ /* 具請?bào)w看線上文檔: fex.baidu.com/ueditor/#use-format_upload_filename */ /* 涂鴉圖片上傳配置項(xiàng) */ "scrawlActionName": "uploadscrawl", /* 執(zhí)行上傳涂鴉的action名稱 */ "scrawlFieldName": "upfile", /* 提交的圖片表單名稱 */ "scrawlPathFormat": "upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳保存路徑,可以自定義保存路徑和文件名格式 */ "scrawlMaxSize": 2048000, /* 上傳大小限制,單位B */ "scrawlUrlPrefix": "/ueditor/net/", /* 圖片訪問路徑前綴 */ "scrawlInsertAlign": "none", /* 截圖工具上傳 */ "snapscreenActionName": "uploadimage", /* 執(zhí)行上傳截圖的action名稱 */ "snapscreenPathFormat": "upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳保存路徑,可以自定義保存路徑和文件名格式 */ "snapscreenUrlPrefix": "/ueditor/net/", /* 圖片訪問路徑前綴 */ "snapscreenInsertAlign": "none", /* 插入的圖片浮動(dòng)方式 */ /* 抓取遠(yuǎn)程圖片配置 */ "catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"], "catcherActionName": "catchimage", /* 執(zhí)行抓取遠(yuǎn)程圖片的action名稱 */ "catcherFieldName": "source", /* 提交的圖片列表表單名稱 */ "catcherPathFormat": "upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳保存路徑,可以自定義保存路徑和文件名格式 */ "catcherUrlPrefix": "/ueditor/net/", /* 圖片訪問路徑前綴 */ "catcherMaxSize": 2048000, /* 上傳大小限制,單位B */ "catcherAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 抓取圖片格式顯示 */ /* 上傳視頻配置 */ "videoActionName": "uploadvideo", /* 執(zhí)行上傳視頻的action名稱 */ "videoFieldName": "upfile", /* 提交的視頻表單名稱 */ "videoPathFormat": "upload/video/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳保存路徑,可以自定義保存路徑和文件名格式 */ "videoUrlPrefix": "/ueditor/net/", /* 視頻訪問路徑前綴 */ "videoMaxSize": 102400000, /* 上傳大小限制,單位B,默認(rèn)100MB */ "videoAllowFiles": [ ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"], /* 上傳視頻格式顯示 */ /* 上傳文件配置 */ "fileActionName": "uploadfile", /* controller里,執(zhí)行上傳視頻的action名稱 */ "fileFieldName": "upfile", /* 提交的文件表單名稱 */ "filePathFormat": "upload/file/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳保存路徑,可以自定義保存路徑和文件名格式 */ "fileUrlPrefix": "/ueditor/net/", /* 文件訪問路徑前綴 */ "fileMaxSize": 51200000, /* 上傳大小限制,單位B,默認(rèn)50MB */ "fileAllowFiles": [ ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid", ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml" ], /* 上傳文件格式顯示 */ /* 列出指定目錄下的圖片 */ "imageManagerActionName": "listimage", /* 執(zhí)行圖片管理的action名稱 */ "imageManagerListPath": "upload/image", /* 指定要列出圖片的目錄 */ "imageManagerListSize": 20, /* 每次列出文件數(shù)量 */ "imageManagerUrlPrefix": "/ueditor/net/", /* 圖片訪問路徑前綴 */ "imageManagerInsertAlign": "none", /* 插入的圖片浮動(dòng)方式 */ "imageManagerAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 列出的文件類型 */ /* 列出指定目錄下的文件 */ "fileManagerActionName": "listfile", /* 執(zhí)行文件管理的action名稱 */ "fileManagerListPath": "upload/file", /* 指定要列出文件的目錄 */ "fileManagerUrlPrefix": "/ueditor/net/", /* 文件訪問路徑前綴 */ "fileManagerListSize": 20, /* 每次列出文件數(shù)量 */ "fileManagerAllowFiles": [ ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid", ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml" ] /* 列出的文件類型 */ }
如果要自定義,下面的配置項(xiàng)必須改
"imageUrlPrefix": "Scripts/ueditor/net/", /* 圖片訪問路徑前綴 */ "imagePathFormat": "upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上傳保存路徑,可以自定義保存路徑和文件名格式 */ "fileUrlPrefix": "/ueditor/net/", /* 文件訪問路徑前綴 */
配置文件 ueditor.config.js 下面的
window.UEDITOR_HOME_URL 可以不指定也可以指定,不指定的話,那就在MVC視圖里面指定,如下所示
...... <script type="text/javascript"> var editor = new baidu.editor.ui.Editor({ UEDITOR_HOME_URL: '/Scripts/ueditor/',//配置編輯器路徑 iframeCssUrl: '/Scripts/ueditor/themes/iframe.css',//樣式路徑 ...... </script> ......
總之就是下面幾個(gè)步驟
1.下載UEditor插件,把文件夾放在項(xiàng)目根目錄下,改名,省事的話就跟配置文件保持一致。
2.建立一個(gè)Controller, Action方法加上 [ValidateInput(false)]
public class UEditorController : Controller { [ValidateInput(false)] // GET: UEditor public ActionResult Index(FormCollection fc) { } }
3.建立視圖,視圖名稱跟Controller的Action方法名稱一致
<script src="~/Scripts/ueditor/ueditor.config.js"></script> <script src="~/Scripts/ueditor/ueditor.all.min.js"></script> <link href="~/Scripts/ueditor/themes/iframe.css" rel="stylesheet" /> <script src="~/Scripts/ueditor/lang/zh-cn/zh-cn.js"></script> @{ ViewBag.Title = "UEditorDemo"; } <script type="text/javascript"> var editor = new baidu.editor.ui.Editor({ UEDITOR_HOME_URL: '/Scripts/ueditor/',//配置編輯器路徑 iframeCssUrl: '/Scripts/ueditor/themes/iframe.css',//樣式路徑 initialContent: '',//初始化編輯器內(nèi)容 autoHeightEnabled: true,//高度自動(dòng)增長 minFrameHeight: 500,//最小高度 autoFloatEnabled: true, initialFrameWidth: 690, initialFrameHeight:483 }); editor.render('editor'); </script> @using (Html.BeginForm("Index", "UEditor", FormMethod.Post)) { <textarea id="editor" name="editor"></textarea> <input type="submit" value="提交" /> }
4.修改Controller如下
public ActionResult Index(FormCollection fc) { var content = fc["editor"]; return View(); }
5.首頁彈出UEditor的頁面用來測試,修改App_Start文件夾下的RouteConfig
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", //defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } defaults: new { controller = "UEditor", action = "Index", id = UrlParameter.Optional } ); } }
6.默認(rèn)我們并不需要UEditor工具欄上的所有按鈕,需要屏蔽一些,這個(gè)時(shí)候需要修改ueditor.config.js
, toolbars: [[ //'fullscreen', 'source', '|', 'undo', 'redo', '|', //'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|', //'rowspacingtop', 'rowspacingbottom', 'lineheight', '|', //'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|', //'directionalityltr', 'directionalityrtl', 'indent', '|', //'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|', //'link', 'unlink', 'anchor', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|', //'simpleupload', 'insertimage', 'emotion', 'scrawl', 'insertvideo', 'music', 'attachment', 'map', 'gmap', 'insertframe', 'insertcode', 'webapp', 'pagebreak', 'template', 'background', '|', //'horizontal', 'date', 'time', 'spechars', 'snapscreen', 'wordimage', '|', //'inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols', 'charts', '|', //'print', 'preview', 'searchreplace', 'drafts', 'help' 'undo', 'redo', '|', 'bold', 'forecolor', 'removeformat', 'autotypeset', 'pasteplain', '|', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'link', 'unlink', '|', 'simpleupload', 'insertimage', '|', 'wordimage', '|', 'inserttable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'splittocells' ]]
如上,我把原始的工具欄配置項(xiàng)注釋掉了,運(yùn)行一下出現(xiàn)如下效果
已經(jīng)少了不少平常用不上的按鈕,好了大功告成~!
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家學(xué)習(xí)或者使用Asp.net能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
.net面向?qū)ο笾嗑€程(Multithreading)及 多線程高級(jí)應(yīng)用
這篇文章主要介紹.net面向?qū)ο蟪绦蛟O(shè)計(jì)階段多線程Multithreading及多線程高級(jí)應(yīng)用的介紹,需要的朋友可以參考下2015-07-07.net?6?配置QuartZ定時(shí)任務(wù)的過程
這篇文章主要介紹了.net?6?配置QuartZ定時(shí)任務(wù)的過程,在VS2022中,通過Nuget包管理器安裝Quartz?3.8.1 ,這是.net 6 依賴的最高版本,在此記錄學(xué)習(xí)一下,需要的朋友可以參考下2024-04-04使用pdfbox實(shí)現(xiàn)pdf文本提取和合并功能示例
這篇文章主要介紹了使用pdfbox實(shí)現(xiàn)pdf文本提取和合并功能示例,大家參考使用吧2014-01-01asp.net利用存儲(chǔ)過程實(shí)現(xiàn)模糊查詢示例分享
這篇文章主要介紹了asp.net利用存儲(chǔ)過程實(shí)現(xiàn)模糊查詢的示例,大家參考使用吧2014-01-01.net連接oracle的3種實(shí)現(xiàn)方法
這篇文章介紹了.net連接oracle的3種實(shí)現(xiàn)方法,有需要的朋友可以才可以一下2013-07-07