I have used several editor, still prefer Kindeditor. This job is probably going to be used recently, and it's time to take a look at home on weekends. Made a note here for future convenience.
1. First go to Kindeditor's official website to download the latest version, and then in the MVC content folder under the download of the package to put in.
2. Then look at the view to make a reference, show
@{Layout = null;}<!DOCTYPE HTML><HTML><Head> <Metaname= "Viewport"content= "Width=device-width" /> <title>Index</title> <Linkrel= "stylesheet"href= "/content/kindeditor/themes/default/default.css" /> <ScriptCharSet= "Utf-8"src= "/content/kindeditor/kindeditor-min.js"></Script> <ScriptCharSet= "Utf-8"src= "/content/kindeditor/lang/zh_cn.js"></Script> <Script> varEditor; Kindeditor.ready (function(K) {editor=K.create ('textarea[name= "Txtcontent"]', {resizetype:1, Uploadjson:'@Url. Action ("UploadImages", "Default")' }); }); </Script></Head><Body> <Div> <formID= "Formguestbook"Method= "POST"Action= "/default/add"> <!--class name/method name -Name:<inputtype= "text"ID= "Txtname"name= "Txtname" /><BR/>content:<textareaID= "Txtcontent"name= "Txtcontent"style= "Width:800px;height:400px;visibility:hidden;"></textarea> <inputtype= "Submit"name= "Btnsubmitguestbook"value= "Submit Message" /> </form> </Div></Body></HTML>
3. After the submission of the controller after the report error, the following errors:
The solution is simple, add [ValidateInput (false)] before the controller's method, as shown in the following code:
[ValidateInput (false)] Public ActionResult Add (stringstring txtcontent) { return View (); }
4. Finally changed from the official website to bring the image upload function, the original is WebForm handler write. changed to MVC. Reference Litjson before use
PublicActionResult UploadImages () {//String aspxurl = HttpContext.Request.Path.Substring (0, HttpContext.Request.Path.LastIndexOf ("/") + 1);String Aspxurl =""; //File Save directory pathString Savepath ="/content/kindeditor/attached/"; //file save directory URLString Saveurl = Aspxurl +"/content/kindeditor/attached/"; //define the file extensions that are allowed to be uploadedHashtable exttable =NewHashtable (); Exttable.add ("Image","gif,jpg,jpeg,png,bmp"); Exttable.add ("Flash","swf,flv"); Exttable.add ("Media","SWF,FLV,MP3,WAV,WMA,WMV,MID,AVI,MPG,ASF,RM,RMVB"); Exttable.add ("file","doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2"); //Maximum file size intMaxSize =1000000; //this.context = HttpContext;HttpPostedFileBase imgfile= httpcontext.request.files["Imgfile"]; if(Imgfile = =NULL) {ShowError ("Please select a file. "); } String Dirpath=HttpContext.Server.MapPath (Savepath); if(!directory.exists (Dirpath)) {ShowError ("The upload directory does not exist. "); } String dirName= httpcontext.request.querystring["dir"]; if(String.IsNullOrEmpty (dirName)) {DirName="Image"; } if(!Exttable.containskey (DirName)) {ShowError ("The directory name is incorrect. "); } String fileName=Imgfile.filename; String Fileext=path.getextension (fileName). ToLower (); if(Imgfile.inputstream = =NULL|| ImgFile.InputStream.Length >maxSize) {ShowError ("The upload file size exceeds the limit. "); } if(String.IsNullOrEmpty (fileext) | | Array.indexof (((String) exttable[dirname]). Split (','), Fileext.substring (1). ToLower ()) = =-1) {ShowError ("the upload file name extension is not allowed. \ n Allow only"+ ((String) exttable[dirname]) +"format. "); } //Create a folderDirpath + = DirName +"/"; Saveurl+ = DirName +"/"; if(!directory.exists (Dirpath)) {directory.createdirectory (Dirpath); } String Ymd= DateTime.Now.ToString ("YYYYMMDD", Datetimeformatinfo.invariantinfo); Dirpath+ = Ymd +"/"; Saveurl+ = Ymd +"/"; if(!directory.exists (Dirpath)) {directory.createdirectory (Dirpath); } String NewFileName= DateTime.Now.ToString ("yyyymmddhhmmss_ffff", Datetimeformatinfo.invariantinfo) +Fileext; String FilePath= Dirpath +NewFileName; Imgfile.saveas (FilePath); String FILEURL= Saveurl +NewFileName; Hashtable Hash=NewHashtable (); hash["Error"] =0; hash["URL"] =FileUrl; returnContent (Jsonmapper.tojson (hash)); //return View (); } PrivateContentresult ShowError (stringmessage) {Hashtable hash=NewHashtable (); hash["Error"] =1; hash["message"] =message; returnContent (Jsonmapper.tojson (hash)); }
Use of ASP. Mvc-kindeditor