Http://www.cnblogs.com/weicong/archive/2012/03/31/2427608.html
The first step
Adding kindeditor source files to your project is recommended in the/scripts/kindeditor directory, where only the lang directory, Plugis directory, themes directory, and kindeditor-min.js files are required.
Step Two
Add a partial View "kindeditor.cshtml" (the filename is optional) in the/views/shared/editortemplates directory. The code is as follows:
1234567891011121314151617181920 |
<
script
type="text/javascript" src="http://mce_host/weicong/admin/@Url.Content("></
script
>
<
script type="text/javascript" src="http://mce_host/weicong/admin/@Url.Content("></
script
>
<
script
type="text/javascript">// <![CDATA[
(function () {
KindEditor.ready(function (k) {
k.create("#@ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty)", {
themeType: ‘default‘,
width: ‘690px‘,
height: ‘400px‘,
uploadJson: ‘/KindEditorHandler/Upload‘,
allowFileManager: true,
fileManagerJson: ‘/KindEditorHandler/FileManager‘
});
});
})();
// ]]></
script
> @Html.TextArea(string.Empty, ViewData.TemplateInfo.FormattedModelValue)
|
Step Three
Set the dataannotations in the model property where you want to apply the editor, such as:
1234 |
[DisplayName( "正文" )] [AllowHtml] [UIHint( "kindeditor" )] // EditorTemplates 目录中添加的视图名称 public object Content { get ; set ; } |
Fourth Step
Use the @Html. Editorfor (model = model) in the view. Content) to load the editor.
Attached Kindeditorhandlercontroller source code
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Globalization;
5 using System.IO;
6 using System.Text.RegularExpressions;
7 using SYSTEM.WEB.MVC;
8
9 namespace Kwdoctorcourse.controllers
10 {
public class Kindeditorhandlercontroller:controller
12 {
13//File Save directory path
The const string savepath = "/uploadfile/";
15
#region Uploadjson
17
18//
+//GET:/kindeditorhandler/upload
20
Public ActionResult Upload ()
22 {
23////File Save directory path
//const string savepath = "/content/uploads/";
25
26//File Save directory URL
var saveurl = Savepath;
28
29//Define the file extensions that are allowed to be uploaded
var exttable = new Hashtable
31 {
{"Image", "Gif,jpg,jpeg,png,bmp"},
{"Flash", "swf,flv"},
{"Media", "SWF,FLV,MP3,WAV,WMA,WMV,MID,AVI,MPG,ASF,RM,RMVB"},
{"File", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2"}
36};
37
38//Maximum file size
maxSize const INT = 2000000;
40
imgfile var = request.files["Imgfile"];
42
if (Imgfile = = null)
44 {
Return ShowError ("Please select File. ");
46}
47
var Dirpath = Server.MapPath (Savepath);
if (! Directory.Exists (Dirpath))
50 {
Wuyi//return showerror ("The upload directory does not exist. "+ Dirpath);
Directory.CreateDirectory (Dirpath);
53}
54
dirName var = request.querystring["dir"];
if (String.IsNullOrEmpty (dirName))
57 {
DirName = "image";
59}
60
if (!exttable.containskey (dirName))
62 {
ShowError return ("The directory name is incorrect. ");
64}
65
var fileName = Imgfile.filename;
Extension var = path.getextension (FileName);
if (Extension = = null)
69 {
ShowError return ("extension = = null");
71}
72
var fileext = extension. ToLower ();
74
if (Imgfile.inputstream = = NULL | | imgFile.InputStream.Length > MaxSize)
76 {
ShowError return ("Upload file size exceeds limit. ");
78}
79
if (String.IsNullOrEmpty (fileext) | |
Bayi Array.indexof ((String) exttable[dirname]). Split (', '), fileext.substring (1). ToLower ()) = =-1)
82 {
ShowError return ("The upload file extension is not allowed. \ n allows only "+ ((String) exttable[dirname]) +" format. ");
84}
85
86//Create folder
Dirpath + = DirName + "/";
Saveurl + = DirName + "/";
if (! Directory.Exists (Dirpath))
90 {
Directory.CreateDirectory (Dirpath);
92}
YMD var = DateTime.Now.ToString ("YyyyMMdd", datetimeformatinfo.invariantinfo);
94 Dirpath + = Ymd + "/";
Saveurl + = Ymd + "/";
if (! Directory.Exists (Dirpath))
97 {
98 directory.createdirectory (Dirpath);
99}
100
101 var newfilename = DateTime.Now.ToString ("Yyyymmddhhmmss_ffff", Datetimeformatinfo.invariantinfo) + Fileext ;
102 var FilePath = Dirpath + newfilename;
103
104 Imgfile.saveas (FilePath);
105
106 var fileUrl = Saveurl + newfilename;
107
108 var hash = new Hashtable ();
109 hash["Error"] = 0;
hash["url"] = FILEURL;
111
Return Json (hash, "text/html;charset=utf-8");
113}
114
Jsonresult private ShowError (String message)
116 {
117 var hash = new Hashtable ();
118 hash["error"] = 1;
119 hash["message"] = message;
120
121 return Json (hash, "text/html;charset=utf-8");
122}
123
124 #endregion
125
126 #region Filemanagerjson
127
128//
129//GET:/kindeditorhandler/filemanager
130
131 public ActionResult FileManager ()
132 {
133////root directory path, relative path
134//string RootPath = "/content/uploads/";
135
136//root directory URL, you can specify an absolute path, such as http://www.yoursite.com/attached/
137 var rooturl = Savepath;
138
139//Image extension
FileTypes const STRING = "Gif,jpg,jpeg,png,bmp";
141
142 String Currentpath;
143 String Currenturl;
144 String Currentdirpath;
145 String Moveupdirpath;
146
147 var Dirpath = Server.MapPath (Savepath);
148 var dirName = request.querystring["dir"];
149 if (! String.IsNullOrEmpty (DirName))
150 {
151 if (Array.indexof ("Image,flash,media,file"). Split (', '), dirName) = =-1)
152 {
153 return Content ("Invalid Directory name.");
154}
155 Dirpath + = DirName + "/";
156 Rooturl + = DirName + "/";
157 if (! Directory.Exists (Dirpath))
158 {
159 Directory.CreateDirectory (Dirpath);
160}
161}
162
163//Set each path and URL according to the path parameter
164 var path = request.querystring["path"];
165 Path = String.IsNullOrEmpty (path)? "": path;
166 if (Path = = "")
167 {
168 Currentpath = Dirpath;
169 Currenturl = Rooturl;
Currentdirpath = "";
171 Moveupdirpath = "";
172}
173 Else
174 {
175 Currentpath = Dirpath + path;
176 Currenturl = Rooturl + path;
177 Currentdirpath = path;
178 Moveupdirpath = Regex.Replace (Currentdirpath, @ "(. *?) [^\/]+\/$ "," $ ");
179}
180
181//Sort form, name or size or type
182 String order = request.querystring["Order"];
183 order = String.IsNullOrEmpty (order)? "": Order. ToLower ();
184
185//Not allowed to use: Move to the top level directory
186 if (Regex.IsMatch (Path, @ "\.\."))
187 {
188 return Content ("Access is not allowed.");
189}
190
191//Last character not/
192 if (Path! = "" "&&!path. EndsWith ("/"))
193 {
194 return Content ("Parameter is not valid.");
195}
196//directory does not exist or is not a directory
197 if (! Directory.Exists (Currentpath))
198 {
199 return Content ("Directory does not exist.");
200}
201
202//Traverse directory to get file information
203 string[] Dirlist = directory.getdirectories (Currentpath);
204 string[] FileList = Directory.GetFiles (Currentpath);
205
206 Switch (order)
207 {
208 case "Size":
209 Array.Sort (Dirlist, New Namesorter ());
Array.Sort (FileList, New Sizesorter ());
211 break;
212 case "Type":
213 Array.Sort (Dirlist, New Namesorter ());
214 Array.Sort (FileList, New Typesorter ());
215 break;
216 Default:
217 Array.Sort (Dirlist, New Namesorter ());
218 Array.Sort (FileList, New Namesorter ());
219 break;
220}
221
222 var result = new Hashtable ();
223 result["Moveup_dir_path"] = Moveupdirpath;
224 result["Current_dir_path"] = Currentdirpath;
225 result["Current_url"] = Currenturl;
226 result["Total_count"] = dirlist.length + filelist.length;
227 var dirfilelist = new list228 result["file_list"] = dirfilelist;
229 foreach (Var t in Dirlist)
230 {
231 var dir = new DirectoryInfo (t);
232 var hash = new Hashtable ();
233 hash["Is_dir"] = true;
234 hash["Has_file"] = (dir. Getfilesysteminfos (). Length > 0);
235 hash["FileSize"] = 0;
236 hash["Is_photo"] = false;
237 hash["filetype"] = "";
238 hash["filename"] = dir. Name;
239 hash["datetime"] = dir. Lastwritetime.tostring ("Yyyy-mm-dd HH:mm:ss");
Dirfilelist.add (hash);
241}
242 foreach (Var t in FileList)
243 {
244 var file = new FileInfo (t);
245 var hash = new Hashtable ();
246 hash["Is_dir"] = false;
247 hash["Has_file"] = false;
248 hash["FileSize"] = file. Length;
249 hash["Is_photo" = (Array.indexof (' Filetypes.split '), file. Extension.substring (1). ToLower ()) >= 0);
hash["filetype"] = file. Extension.substring (1);
251 hash["filename"] = file. Name;
252 hash["datetime"] = file. Lastwritetime.tostring ("Yyyy-mm-dd HH:mm:ss");
253 Dirfilelist.add (hash);
254}
255
The return Json (result, "Text/html;charset=utf-8", jsonrequestbehavior.allowget);
257}
258
259
260 Private Class Namesorter:icomparer
261 {
262 public int Compare (object x, Object y)
263 {
if (x = = null && y = = null)
265 {
266 return 0;
267}
268 if (x = = null)
269 {
return-1;
271}
272 if (y = = null)
273 {
274 return 1;
275}
276 var xinfo = new FileInfo (x.tostring ());
277 var yinfo = new FileInfo (y.tostring ());
278
279 return string.compareordinal (Xinfo.fullname, yinfo.fullname);
280}
281}
282
283 Private class Sizesorter:icomparer
284 {
285 public int Compare (object x, Object y)
286 {
287 if (x = = null && y = = null)
288 {
289 return 0;
290}
291 if (x = = null)
292 {
293 return-1;
294}
295 if (y = = null)
296 {
297 return 1;
298}
299 var xinfo = new FileInfo (x.tostring ());
var yinfo = new FileInfo (y.tostring ());
301
302 return XInfo.Length.CompareTo (yinfo.length);
303}
304}
305
306 Private Class Typesorter:icomparer
307 {
308 public int Compare (object x, Object y)
309 {
310 if (x = = null && y = = null)
311 {
312 return 0;
313}
314 if (x = = null)
315 {
return-1;
317}
318 if (y = = null)
319 {
1;
321}
322 var xinfo = new FileInfo (x.tostring ());
323 var yinfo = new FileInfo (y.tostring ());
324
325 return string.compareordinal (xinfo.extension, yinfo.extension);
326}
327}
328
329 #endregion
330}
331}
Applying kindeditor in ASP. NET MVC 3