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 = ;
var context = Request.HttpContext;
var imgFile = Request.Form.Files[]; //文件类型
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().ToLower()) == -)
{
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"] = ;
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"] = ;
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="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>

改造kindeditor支持asp.net core mvc上传文件的更多相关文章

  1. ASP.NET Core MVC上传文件

    使用模型绑定上传小文件 HTML代码: <form method="post" enctype="multipart/form-data" asp-con ...

  2. ASP.NET Core MVC上传、导入、导出知多少

    前言 本君已成夜猫子,本节我们来讲讲ASP.NET Core MVC中的上传,这两天才研究批量导入功能,本节顺便简单搞搞导入.导出,等博主弄妥当了再来和大家一并分享. .NET Core MVC上传 ...

  3. asp.net core mvc上传大文件解决方案

    默认上传文件大小不超过30M 第一个问题: IIS 10.0 详细错误 - 404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求. 服务器上的请求筛选被配置为拒绝该请求 ...

  4. asp.net core分块上传文件

    写完asp.net多文件上传(http://www.cnblogs.com/bestckk/p/5987383.html)后,感觉这种上传还是有很多缺陷,于是...(省略一万字,不废话).这里我没用传 ...

  5. asp.net MVC 上传文件 System.Web.HttpException: 超过了最大请求长度

    APS.NET MVC 上传文件出现  System.Web.HttpException: 超过了最大请求长度 这个问题 原因是 默认最大上传文件大小为4096,而我提交的文件太大了. 解决方案:修改 ...

  6. net core分块上传文件

    net core分块上传文件   写完asp.net多文件上传(http://www.cnblogs.com/bestckk/p/5987383.html)后,感觉这种上传还是有很多缺陷,于是...( ...

  7. Spring MVC上传文件

    Spring MVC上传文件 1.Web.xml中加入 <servlet> <servlet-name>springmvc</servlet-name> <s ...

  8. MVC上传文件

    ASP.NET MVC上传文件是必段撑握的知识.加强训练才是.以前Insus.NET曾使用第三方MyAjaxForm.js :http://www.cnblogs.com/insus/p/378548 ...

  9. 一、.Net Core 分块上传文件

    一..Net Core 分块上传文件 一.前端实现 @* For more information on enabling MVC for empty projects, visit http://g ...

随机推荐

  1. 4-3 重构发送post请求

    当创建好一个post请求后 怎么测试呢?

  2. linux常用命令简介

    不管是测试还是开发,平时或多或少都要用到Linux命令,下面就把平时必用的一些命令简单总结哈,快学快用 1.   ls : 列举当前目录下文件.子目录的名字,如图举例: (1)  ls  -l  : ...

  3. STS中web.xml配置文件

    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" " ...

  4. Sublime text 2/3 [Decode error - output not utf-8] 完美解决方法

    原文链接 http://blog.csdn.net/bbdxf/article/details/25594703 [Decode error - output not utf-8]或者[Decode  ...

  5. angularjs ng-bind-html的用法总结

    angular中的$sanitize服务. 此服务依赖于ngSanitize模块.(这个模块需要加载angular-sanitize.js插件) 要学习这个服务,先要了解另一个指令: ng-bing- ...

  6. Python几周学习内容小结

    环境配置 学习python首先是要配置环境,我们选择了Anaconda. 什么是Anaconda:专注于数据分析的python发行版本. 为什么选择Anaconda:省事省心,分析利器 至于下载和安装 ...

  7. 人力资源项目中 add_account.php

    add_account.php ( 文件浏览 ) <?phpinclude('db_con.php');   if(isset($_POST['save'])) {    $employee_i ...

  8. 使用kbmmw smarthttpservice 简单返回数据库结果

    这个很简单,直接上码. 服务器端声明过程 [kbmMW_Rest('method:get, path:querytable')] [kbmMW_Method] function querytable( ...

  9. Windows端口开放

    1.查看:cmd->netstat -na: 2.测试:cmd->telnet [ip] [port]: 3.开启:防火墙新建规则.

  10. HDU 2516 斐波那契博弈

    点这里去看题 n为斐波那契数时,先手败,推断方法见算法讲堂 #include<bits/stdc++.h> using namespace std; int main() { ],i,n, ...