kindtor默认使用的上传方法是使用目录下面的一般处理程序upload_json.ashx,暂时还不支持asp.net core下的文件上传,下面放出的自定义处理上传文件的接口方法。
自定义接收上传文件的action替换一般处理程序
代码如下:
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("上传文件扩展名是不允许的扩展名。 只允许" + ((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代码
@{ 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="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>