asp.net core集成kindeditor實(shí)現(xiàn)圖片上傳功能
本文為大家分享了asp.net core 如何集成kindeditor并實(shí)現(xiàn)圖片上傳功能的具體方法,供大家參考,具體內(nèi)容如下
準(zhǔn)備工作
1.visual studio 2015 update3開(kāi)發(fā)環(huán)境
2.net core 1.0.1 及以上版本
目錄
新建asp.net core web項(xiàng)目
下載kindeditor
增加圖片上傳控制器
配置kindeditor參數(shù)
代碼下載
新建asp.net core web項(xiàng)目
新建一個(gè)asp.net core項(xiàng)目,這里命名為kindeditor

選中web應(yīng)用程序

下載kindeditor
這里我們新建了一個(gè)系統(tǒng)自帶的樣本項(xiàng)目,去 kindeditor官網(wǎng)下載一個(gè)版本,解壓后拷貝大wwwroot中

修改views/index.cshtml
@{
ViewData["Title"] = "Home Page";
}
<link href="~/kindeditor/themes/default/default.css" rel="stylesheet" />
<script src="~/kindeditor/kindeditor-min.js"></script>
<script src="~/kindeditor/lang/zh_CN.js"></script>
<div class="row">
<textarea id="detail_desc" name="detail_desc" style="width:700px;height:300px;">
</textarea>
</div>
<script type="text/javascript">
//實(shí)例化編輯器
//建議使用工廠方法getEditor創(chuàng)建和引用編輯器實(shí)例,如果在某個(gè)閉包下引用該編輯器,直接調(diào)用UE.getEditor('editor')就能拿到相關(guān)的實(shí)例
KindEditor.ready(function (K) {
window.editor = K.create('#detail_desc', {
width: '98%',
height: '500px'
});
});
</script>
運(yùn)行一下現(xiàn)在就可以看到kindeditor已經(jīng)集成進(jìn)來(lái)了。

增加圖片上傳控制器
注意返回是一個(gè)json對(duì)象,因此建了一個(gè)簡(jiǎn)單的對(duì)象返回。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
using Microsoft.AspNetCore.Hosting;
using System.IO;
namespace kindeditortest.Controllers
{
public class HomeController : Controller
{
private IHostingEnvironment hostingEnv;
public IActionResult Index()
{
return View();
}
public HomeController(IHostingEnvironment env)
{
this.hostingEnv = env;
}
/// <summary>
/// Kindeditor圖片上傳
/// </summary>
/// <param name="imgFile">Kindeditor圖片上傳自帶的命名,不可更改名稱</param>
/// <param name="dir">不可更改名稱 這里沒(méi)有用到dir</param>
/// <returns></returns>
public IActionResult KindeditorPicUpload(IList<IFormFile> imgFile, string dir)
{
PicUploadResponse rspJson = new PicUploadResponse() { error = 0, url = "/upload/" };
long size = 0;
string tempname = "";
foreach (var file in imgFile)
{
var filename = ContentDispositionHeaderValue
.Parse(file.ContentDisposition)
.FileName
.Trim('"');
var extname = filename.Substring(filename.LastIndexOf("."), filename.Length - filename.LastIndexOf("."));
var filename1 = System.Guid.NewGuid().ToString() + extname;
tempname = filename1;
var path = hostingEnv.WebRootPath;
filename = hostingEnv.WebRootPath + $@"\upload\{filename1}";
size += file.Length;
using (FileStream fs = System.IO.File.Create(filename))
{
file.CopyTo(fs);
fs.Flush();
//這里是業(yè)務(wù)邏輯
}
}
rspJson.error = 0;
rspJson.url = $@"../../upload/" + tempname;
return Json(rspJson);
}
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";
return View();
}
public IActionResult Contact()
{
ViewData["Message"] = "Your contact page.";
return View();
}
public IActionResult Error()
{
return View();
}
}
public class PicUploadResponse
{
public int error { get; set; }
public string url { get; set; }
}
}
配置kindeditor參數(shù)
<script type="text/javascript">
//實(shí)例化編輯器
//建議使用工廠方法getEditor創(chuàng)建和引用編輯器實(shí)例,如果在某個(gè)閉包下引用該編輯器,直接調(diào)用UE.getEditor('editor')就能拿到相關(guān)的實(shí)例
KindEditor.ready(function (K) {
window.editor = K.create('#detail_desc', {
width: '98%',
height: '500px',
uploadJson: '/home/KindeditorPicUpload',
fileManagerJson: '/home/KindeditorPicUpload',
allowFileManager: true
});
});
</script>
運(yùn)行效果


源碼下載:http://xiazai.jb51.net/201611/yuanma/ASP.NETkindeditor(jb51.net).rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET Core 7 Razor Pages項(xiàng)目發(fā)布到IIS的詳細(xì)過(guò)程
這篇文章主要介紹了ASP.NET Core 7 Razor Pages項(xiàng)目發(fā)布到IIS的詳細(xì)過(guò)程,詳細(xì)介紹了發(fā)布過(guò)程遇到的問(wèn)題及解決方法,對(duì)ASP.NET Core 發(fā)布到IIS相關(guān)知識(shí)感興趣的朋友一起看看吧2023-01-01
Entity?Framework?Core基于數(shù)據(jù)模型創(chuàng)建數(shù)據(jù)庫(kù)
這篇文章介紹了Entity?Framework?Core基于數(shù)據(jù)模型創(chuàng)建數(shù)據(jù)庫(kù)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
ASP.net基礎(chǔ)知識(shí)之常見(jiàn)錯(cuò)誤分析
ASP.net基礎(chǔ)知識(shí)之常見(jiàn)錯(cuò)誤分析...2007-07-07
asp.net jQuery Ajax用戶登錄功能的實(shí)現(xiàn)
前幾天把jbox源碼修改成仿QQ空間模擬窗口后發(fā)現(xiàn)有很多人在關(guān)注。今天就貼一下我利用該模擬窗口實(shí)現(xiàn)的用戶登錄功能的代碼。2009-11-11
C#基礎(chǔ)之?dāng)?shù)據(jù)類型轉(zhuǎn)換
簡(jiǎn)單認(rèn)識(shí)顯式轉(zhuǎn)換和隱式轉(zhuǎn)換 我們就從下面這段代碼段開(kāi)始吧2013-02-02
MVC4制作網(wǎng)站教程第三章 添加用戶組操作3.2
這篇文章主要為大家詳細(xì)介紹了MVC4制作網(wǎng)站教程,添加用戶組功能的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
asp.net的web頁(yè)面(aspx)數(shù)據(jù)量過(guò)多時(shí)提交失敗對(duì)策
asp.net的web頁(yè)面,數(shù)據(jù)量過(guò)多時(shí)提交失敗的情況想必有很多朋友都有遇到過(guò)吧,下面與大家分享下詳細(xì)的解決方法2013-05-05

