Use of ASP. NET Mvc-kindeditor

Source: Internet
Author: User
Tags tojson

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.