MVC KindEdit, mvckindedit

Source: Internet
Author: User

MVC KindEdit, mvckindedit

Put the downloaded KindEditor in the project.

View page

<! DOCTYPE html>
<Html>
<Head>
<Meta name = "viewport" content = "width = device-width"/>
<Title> Index </title>
@ Scripts. Render ("~ /Bundles/kindeditor ") // MVC4 method to load kindeditor/kindeditor. js
<Script type = "text/javascript">
Var editor;
KindEditor. ready (function (K ){
Editor = K. create ('textarea [name = "Information"] ', {
AllowFileManager: true, // whether to browse and upload files
AllowUpload: true, // whether the file can be uploaded
FileManagerJson: '/KindEditor/processrequest', // file browsing Method
UploadJson: '/KindEditor/uploadimag' // File Upload method // note the two paths
});
});
</Script>
</Head>
<Body>
@ Using (Html. BeginForm ())
{
@ Html. TextArea ("Information", new {style = "width: 800px; height: 400px "})
<Input type = "submit" value = "Submit"/>
<Hr/>
@ Html. Raw (ViewData ["kindeditor"])
}

<% -- <% Html. BeginForm (); %> This is the way MVC3 is written above MVC4
<Textarea name = "Information" style = "width: 800px; height: 400px"> </textarea>
<Input type = "submit" value = "Submit"/>
<Hr/>
<%: ViewData ["kindeditor"] %>
<% Html. EndForm (); %> -- %>
</Body>
</Html>

 

Controller:
[ValidateInput (false)] // an error is returned if no submission is added.
Public ActionResult Index (string Information)
{
ViewData ["kindeditor"] = Information;
Return View ();
}

 

Upload method:
[HttpPost]
Public ActionResult UploadImage ()
{
String savePath = "/UploadImages /";
String saveUrl = "/UploadImages /";
String fileTypes = "gif, jpg, jpeg, png, bmp ";
Int maxSize = 1000000;

Hashtable hash = new Hashtable ();

HttpPostedFileBase file = Request. Files ["imgFile"];
If (file = null)
{
Hash = new Hashtable ();
Hash ["error"] = 1;
Hash ["message"] = "select a file ";
Return Json (hash, "text/html; charset = UTF-8 ");
}

String dirPath = Server. MapPath (savePath );
If (! Directory. Exists (dirPath ))
{
Hash = new Hashtable ();
Hash ["error"] = 1;
Hash ["message"] = "the upload directory does not exist ";
Return Json (hash, "text/html; charset = UTF-8 ");
}

String fileName = file. FileName;
String fileExt = Path. GetExtension (fileName). ToLower ();

ArrayList fileTypeList = ArrayList. Adapter (fileTypes. Split (','));

If (file. InputStream = null | file. InputStream. Length> maxSize)
{
Hash = new Hashtable ();
Hash ["error"] = 1;
Hash ["message"] = "the size of the uploaded file exceeds the limit ";
Return Json (hash, "text/html; charset = UTF-8 ");
}

If (string. IsNullOrEmpty (fileExt) | Array. IndexOf (fileTypes. Split (','), fileExt. Substring (1). ToLower () =-1)
{
Hash = new Hashtable ();
Hash ["error"] = 1;
Hash ["message"] = "uploading file extensions are not allowed ";
Return Json (hash, "text/html; charset = UTF-8 ");
}

String newFileName = DateTime. Now. ToString ("yyyyMMddHHmmss_ffff", DateTimeFormatInfo. InvariantInfo) + fileExt;
String filePath = dirPath + newFileName;
File. SaveAs (filePath );
String fileUrl = saveUrl + newFileName;

Hash = new Hashtable ();
Hash ["error"] = 0;
Hash ["url"] = fileUrl;

Return Json (hash, "text/html; charset = UTF-8 ");
}

Browsing method:
Public ActionResult ProcessRequest ()
{
// String aspxUrl = context. Request. Path. Substring (0, context. Request. Path. LastIndexOf ("/") + 1 );

// Root directory path, relative path
String rootPath = "/UploadImages /";
// Root directory URL. You can specify the absolute path,
String rootUrl = "/UploadImages /";
// Image Extension
String fileTypes = "gif, jpg, jpeg, png, bmp ";

String currentPath = "";
String currentUrl = "";
String currentDirPath = "";
String moveupDirPath = "";

// Set the path and URL Based on the path Parameter
String path = Request. QueryString ["path"];
Path = String. IsNullOrEmpty (path )? "": Path;
If (path = "")
{
CurrentPath = Server. MapPath (rootPath );
CurrentUrl = rootUrl;
CurrentDirPath = "";
MoveupDirPath = "";
}
Else
{
CurrentPath = Server. MapPath (rootPath) + path;
CurrentUrl = rootUrl + path;
CurrentDirPath = path;
MoveupDirPath = Regex. Replace (currentDirPath ,@"(.*?) [^ \/] + \/$ "," $1 ");
}

// Sorting format, name or size or type
String order = Request. QueryString ["order"];
Order = String. IsNullOrEmpty (order )? "": Order. ToLower ();

// Cannot be used .. move to the upper-level directory
If (Regex. IsMatch (path ,@"\.\."))
{
Response. Write ("Access is not allowed .");
Response. End ();
}
// The last character is not/
If (path! = ""&&! Path. EndsWith ("/"))
{
Response. Write ("Parameter is not valid .");
Response. End ();
}
// The directory does not exist or is not a directory
If (! Directory. Exists (currentPath ))
{
Response. Write ("Directory does not exist .");
Response. End ();
}

// Retrieve file information through the Directory
String [] dirList = Directory. GetDirectories (currentPath );
String [] fileList = Directory. GetFiles (currentPath );

Switch (order)
{
Case "size ":
Array. Sort (dirList, new NameSorter ());
Array. Sort (fileList, new SizeSorter ());
Break;
Case "type ":
Array. Sort (dirList, new NameSorter ());
Array. Sort (fileList, new TypeSorter ());
Break;
Case "name ":
Default:
Array. Sort (dirList, new NameSorter ());
Array. Sort (fileList, new NameSorter ());
Break;
}

Hashtable result = new Hashtable ();
Result ["moveup_dir_path"] = moveupDirPath;
Result ["current_dir_path"] = currentDirPath;
Result ["current_url"] = currentUrl;
Result ["total_count"] = dirList. Length + fileList. Length;
List <Hashtable> dirFileList = new List <Hashtable> ();
Result ["file_list"] = dirFileList;
For (int I = 0; I <dirList. Length; I ++)
{
DirectoryInfo dir = new DirectoryInfo (dirList [I]);
Hashtable hash = new Hashtable ();
Hash ["is_dir"] = true;
Hash ["has_file"] = (dir. GetFileSystemInfos (). Length> 0 );
Hash ["filesize"] = 0;
Hash ["is_photo"] = false;
Hash ["filetype"] = "";
Hash ["filename"] = dir. Name;
Hash ["datetime"] = dir. LastWriteTime. ToString ("yyyy-MM-dd HH: mm: ss ");
DirFileList. Add (hash );
}
For (int I = 0; I <fileList. Length; I ++)
{
FileInfo file = new FileInfo (fileList [I]);
Hashtable hash = new Hashtable ();
Hash ["is_dir"] = false;
Hash ["has_file"] = false;
Hash ["filesize"] = file. Length;
Hash ["is_photo"] = (Array. IndexOf (fileTypes. Split (','), file. Extension. Substring (1). ToLower ()> = 0 );
Hash ["filetype"] = file. Extension. Substring (1 );
Hash ["filename"] = file. Name;
Hash ["datetime"] = file. LastWriteTime. ToString ("yyyy-MM-dd HH: mm: ss ");
DirFileList. Add (hash );
}
// Response. AddHeader ("Content-Type", "application/json; charset = UTF-8 ");
// Context. Response. Write (JsonMapper. ToJson (result ));
// Context. Response. End ();
Return Json (result, "text/html; charset = UTF-8", JsonRequestBehavior. AllowGet );
}

Public class NameSorter: IComparer
{
Public int Compare (object x, object y)
{
If (x = null & y = null)
{
Return 0;
}
If (x = null)
{
Return-1;
}
If (y = null)
{
Return 1;
}
FileInfo xInfo = new FileInfo (x. ToString ());
FileInfo yInfo = new FileInfo (y. ToString ());

Return xInfo. FullName. CompareTo (yInfo. FullName );
}
}

Public class SizeSorter: IComparer
{
Public int Compare (object x, object y)
{
If (x = null & y = null)
{
Return 0;
}
If (x = null)
{
Return-1;
}
If (y = null)
{
Return 1;
}
FileInfo xInfo = new FileInfo (x. ToString ());
FileInfo yInfo = new FileInfo (y. ToString ());

Return xInfo. Length. CompareTo (yInfo. Length );
}
}

Public class TypeSorter: IComparer
{
Public int Compare (object x, object y)
{
If (x = null & y = null)
{
Return 0;
}
If (x = null)
{
Return-1;
}
If (y = null)
{
Return 1;
}
FileInfo xInfo = new FileInfo (x. ToString ());
FileInfo yInfo = new FileInfo (y. ToString ());

Return xInfo. Extension. CompareTo (yInfo. Extension );
}
}

 

P.S recently found Json (hash); sometimes there may be problems, use Json (hash, "text/html; charset = UTF-8 ");

 

Source: http://blog.163.com/very_apple/blog/static/277592362012111155310526/

 

The Single Image Upload is to be extracted.

Related Article

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.