kindtor默認使用的上傳方法是使用目錄下面的通常處理程序upload_json.ashx,暫時還不支持asp.net core下的文件上傳,下面放出的自定義處理上傳文件的接口方法。css
自定義接收上傳文件的action替換通常處理程序html
代碼以下:jquery
public class FileController : Controller { private readonly IHostingEnvironment _hostingEnvironment; public FileController(IHostingEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; } public async Task<IActionResult> KindEditorImgUpload() { Dictionary<string, string> extTable = new Dictionary<string, string>(); 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"); //最大文件大小 int maxSize = 1000000; var context = Request.HttpContext; var imgFile = Request.Form.Files[0]; //文件類型 string dirName = Request.Query["dir"]; if (string.IsNullOrEmpty(dirName)) { dirName = "image"; } if (!extTable.ContainsKey(dirName)) { showError("目錄名不正確。"); } String fileName = imgFile.FileName; String fileExt = Path.GetExtension(fileName).ToLower(); if (imgFile== null || imgFile.Length > maxSize) { showError("上傳文件大小超過限制。"); } if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1) { showError("上傳文件擴展名是不容許的擴展名。\n只容許" + ((String)extTable[dirName]) + "格式。"); } string saveDir = Request.Query["saveDir"]; string saveDirStr = null; if (saveDir == null) { saveDirStr = "tmp"; } else { saveDirStr = saveDir.ToString(); } //文件保存目錄 string contentRootPath = _hostingEnvironment.ContentRootPath; string savePath = "/wwwroot/upload/kindeditor/" + dirName + "/" + saveDirStr; string dirPath =contentRootPath +savePath; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } String newFileName = DateTime.Now.ToString("_yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt; String filePath = dirPath + "/" + newFileName; using (FileStream fs = System.IO.File.Create(filePath)) { await imgFile.CopyToAsync(fs); fs.Flush(); } Dictionary<string, object> hash = new Dictionary<string, object>(); hash["url"] = (savePath + "/" + newFileName).Replace("/wwwroot", ""); hash["error"] = 0; Response.Headers.Add("Content-Type", "text/html; charset=UTF-8"); return Json(hash); } private IActionResult showError(string message) { Dictionary<string, object> hash = new Dictionary<string, object>(); hash["error"] = 1; hash["message"] = message; Response.Headers.Add("Content-Type", "text/html; charset=UTF-8"); return Json(hash); } }
對應的kindeditor編輯view代碼json
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <script src="~/lib/jquery/dist/jquery.js"></script> <link href="~/lib/kindeditor-4.1.10/themes/default/default.css" rel="stylesheet" /> <script src="~/lib/kindeditor-4.1.10/kindeditor.js"></script> <script src="~/lib/kindeditor-4.1.10/lang/zh_CN.js"></script> </head> <body> <div class="form-horizontal col-sm-12"> <textarea rows="8" class="form-control" name="Content" id="Content" style="width:950px;height:500px;visibility:hidden;"></textarea> </div> <script> KindEditor.ready(function (K) { window.NewsContent = K.create("#Content", { cssPath: '/lib/kindeditor-4.1.10/plugins/code/prettify.css', uploadJson: '/File/KindEditorImgUpload?saveDir=news_content', fileManagerJson: '/lib/kindeditor-4.1.10/asp.net/file_manager_json.ashx', allowFileManager: true, afterCreate: function () { this.sync(); }, afterBlur: function () { this.sync(); } }); $(".ke-container").addClass("form-control"); }); </script> </body> </html>