You are using an ASP. NET MVC upload image.
1. Download the corresponding package for Kindeditor
2.html page
@{Layout = null;}<!DOCTYPE HTML><HTML><Head> <Metaname= "Viewport"content= "Width=device-width" /> <title>Uploadbykindeditor</title> <Scriptsrc= "~/scripts/jquery-1.8.2.min.js"></Script> <Scriptsrc= "~/content/kindeditor/kindeditor.js"></Script> <Scriptsrc= "~/content/kindeditor/plugins/image/image.js"></Script> <Scripttype= "Text/javascript"> varEditor; varOptions={uploadjson:'/businesspublic/uploadimage', //(Businesspublic,uploadimage for Action, below) upload pictureFilemanagerjson:'/businesspublic/uploadfile', //Uploading FilesAllowfilemanager:true, Width:"100%", //the width of the editor is 100%Height:"250px", //the height of the editor is 100pxFilterMode:false, //does not filter HTML codeResizeMode:1 //Editor can only adjust height }; $(function() {editor=Kindeditor.create ('#content', Options); }); </Script></Head><Body> <Div>content:<textareaID= "Content"name= "Content"style= "height:300px;"></textarea> </Div></Body></HTML>
3. Background action code: use post to submit (upload file is using post mode)
[HttpPost] PublicActionResult Uploadimage () {stringSavepath ="/resource/kindeditorimage/"; stringFileTypes ="gif,jpg,jpeg,png,bmp"; intMaxSize =1000000; Hashtable Hash=NewHashtable (); HttpPostedFileBase file= request.files["Imgfile"]; if(File = =NULL) {Hash=NewHashtable (); hash["Error"] =0; hash["URL"] ="Please select a file"; returnJson (hash); } stringDirpath =Server.MapPath (Savepath); if(!directory.exists (Dirpath)) {directory.createdirectory (Dirpath); } stringFileName =file. FileName; stringFileext =path.getextension (fileName). ToLower (); ArrayList filetypelist= Arraylist.adapter (Filetypes.split (',')); if(file. InputStream = =NULL|| File. Inputstream.length >maxSize) {Hash=NewHashtable (); hash["Error"] =0; hash["URL"] ="upload file size exceeds limit"; returnJson (hash); } if(string. IsNullOrEmpty (fileext) | | Array.indexof (Filetypes.split (','), Fileext.substring (1). ToLower ()) = =-1) {Hash=NewHashtable (); hash["Error"] =0; hash["URL"] ="upload file name extension is not allowed"; returnJson (hash); } stringNewFileName = DateTime.Now.ToString ("yyyymmddhhmmss_ffff", Datetimeformatinfo.invariantinfo) +Fileext; stringFilePath = Dirpath +NewFileName; File. SaveAs (FilePath); //the path of the picture on the server stringFILEURL = Savepath +NewFileName; Hash=NewHashtable (); hash["Error"] =0; hash["URL"] =FileUrl; returnJson (Hash,"Text/html;charset=utf-8"); ; }
Ps:
The image upload function is implemented by Kindeditor: (1) modify. The filename type in the/plugins/image.js file is the name of file (2) and the upload processing Url:var editor is added; Kindeditor.ready (function (K) {editor = k.create (' #myeditor ', {uploadjson: '/uploadimg ') }); });
(3) Returns the JSON information:
Success {"Error": 0, "url": "Http://www.example.com/path/to/file.ext"}
Failure {"Error": 1, "message": "Error Message"}
The following is the old version Setup method (OBSOLETE):------------------------------------------------------------------------------------------------ (1) Modify: The name of the input type file in the/plugins/image.html.
(2) write the server-side image upload method, require the returned result is in JSON format
(3) The JSON format requirements are: {"error": 0, "message": "...", "url": "/img/1111.gif"} where the error value of 0 indicates successful upload, you need to specify the URL value for the image after the saved URL address, If the error value is not 0, set the message value to the error message (4) HTML page://Editor Initialization settings ke.show ({id: ' editor ', Allowupload:true,//allow uploading of images Imageu Ploadjson: '/saveimg '//server upload image processing URI}); This requires a form <textarea id= "editor" name= "Content" style= "width:700px;height:300px;" ></textarea> (5) That's it. Note: Do not forget to import the Plugins/image folder, otherwise the picture upload button is not valid.
Kindeditor Editor Upload image