需求大概是这个样子(项目架构为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