在WEB API项目中使用KindEditor富文本编辑器

需求大概是这个样子(项目架构为WEB API+WEB端+Winform端+Android端):

WEB端和Winform端使用KindEditor来编辑和查看文档内容,文档内容保存到数据库,插入的图片保存到API服务器。保存和上传图片的动作整合到

API中。Android端可以查看文档内容,不过不是我负责就不管了。

先研究API。有中文文档看起来很舒服,不过配套的DEMO太少,文档也不够详细。

http://kindeditor.net/doc.php

再找DEMO。

http://www.cnblogs.com/shaoming01/archive/2012/02/03/winformhtmleditor.html

然后开始往自己的项目里面加东西。

先把下载的压缩包解压加到项目里面,最好放在根目录,这样加引用的时候省事一点。

API:添加一个新控制器,用来上传图片。(其实就是把官方DEMO里面的upload_json.ashx拿过来)

代码里面高亮了一句,这个URL一定要是形如http://abc.com/123.jpg这样可以直接访问的URL,否则编辑器没法读取。

然后服务器上自己建立一下文件夹。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Collections;
using System.Configuration;
using System.Web;
using System.IO;
using System.Globalization;
using LitJson;

namespace DocManagerAPI.Controllers
{
    public class DocImageController : ApiController
    {
        [FormAuth]
        [AcceptVerbs("GET", "POST")]
        [WebApiTracker]
        public void upLoadDocImage()
        {//上传文档内容中的文件
            String aspxUrl = HttpContext.Current.Request.Path.Substring(0, HttpContext.Current.Request.Path.LastIndexOf("/") + 1);

            //文件保存目录路径
            String savePath = "/DocImage/";

            //文件保存目录URL
            String saveUrl = System.Configuration.ConfigurationManager.AppSettings["serverUrl"] + "DocImage/";

            //定义允许上传的文件扩展名
            Hashtable extTable = new Hashtable();
            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;
            //this.ControllerContext =HttpContext.Current.Session;

            HttpPostedFile imgFile = HttpContext.Current.Request.Files["imgFile"];
            if (imgFile == null)
            {
                showError("请选择文件。");
            }

            String dirPath = HttpContext.Current.Server.MapPath(savePath);
            if (!Directory.Exists(dirPath))
            {
                showError("上传目录不存在。");
            }

            String dirName = HttpContext.Current.Request.QueryString["dir"];
            if (String.IsNullOrEmpty(dirName))
            {
                dirName 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值