Kindeditor,一个通用的纯JS轻量级在线编辑器,在官方下载下来的Kindeditor源文件包中,包含KindEditor.js、skins文件夹、upload_cgi文件夹、icons文件夹、attached文件夹、demo文件夹、docs文件夹。
1、skins文件夹:用于放置编辑器皮肤样式的文件夹
2、upload_cgi文件夹:此文件夹放置图片上传所使用的CGI程序文件夹,官方提供的CGI程序有upload.cgi、upload.asp、upload.php这三种图片上传用的CGI程序
3、icons文件夹:此文件夹放置编辑器的所有图标
4、attached文件夹:按照官方的说明,此文件夹是可以用来放置上传的图片。但实际中,图片上传的路径是可以自己设置的
5、demo文件夹:放置了官方提供的实例文件
6、docs文件夹:说明文档所在文件夹
HTML页面中的设置方法,就按照官方提供的说明文档设置就可以,在这里我举例用ASP.NET实现里面的上传功能,因为官方提供的没有.NET的上传类
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Collections.Generic;
using RIF.Utility;
using System.Drawing.Imaging;namespace RIF.WebUI.Editor
{
/// <summary>
/// Author: Richie
/// Created: 2007-12-25
/// Description: WebForm 编辑器(KindEditor)中图片上传
/// Connection:
/// </summary>
public partial class UploadFile : System.Web.UI.Page
{
private string save_Path = HttpContext.Current.Request.MapPath(”/UploadFile/EditorUploadImages/”); //图片上传的目录路径
private string save_Url = PageBase.RootURL + “/UploadFile/EditorUploadImages/”; //图片保存的目录URL
private string[] ext_Arr = new string[] { “.jpg”, “.gif”, “.png”, “.bmp” }; //允许上传的图片类型
private const int max_Size = 1024 * 1024 * 2; //最大图片文件的大小(2MB)
private string imgName; //图片名
private string imgTitle; //图片ALT描述
private string imgWidth; //图片宽度
private string imgHeigth; //图片高度
private string imgBorder; //图片边宽
private string imgAlign; //对齐方式
private string imgHspace; //图片横向空白
private string imgVspace; //图片竖向空白
private string imgPath; //图片全路径
private string imgUrl; //图片URL
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
imgName = Request.Form[”fileName”];
imgTitle = Request.Form[”imgTitle”];
imgWidth = Request.Form[”imgWidth”];
imgHeigth = Request.Form[”imgHeigth”];
imgBorder = Request.Form[”imgBorder”];
imgAlign = Request.Form[”imgAlign”];
imgHspace = Request.Form[”imgHspace”];
imgVspace = Request.Form[”imgVspace”];
imgPath = save_Path + imgName;
imgUrl = save_Url + imgName;
//’遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
HttpPostedFile postedFile = files[0];
UploadImage(postedFile);
}
}
private void UploadImage(HttpPostedFile postedFile)
{
if (postedFile.ContentLength != 0)
{
if (postedFile.ContentLength > max_Size)
{
Alert(”上传的图片大小超过系统规定大小” + max_Size / 1024 + “(MB)”);
}
else if (!Directory.Exists(save_Path))
{
Alert(”上传目录不存在”);
}
else
{
//保存文件到目标位置,并加入水印
//new ThumbnailCreate().ThumbCreate(postedFile, 200, 200);
new ImgWatermark().SetWatermarkToImage(postedFile, save_Path + imgName);
string html = “<html>”;
html += “<head>”;
html += “<title>文件上传</title>”;
html += “<meta http-equiv=\”content-type\” content=\”text/html; charset=utf-8\”>”;
html += “</head>”;
html += “<body>”;
html += “<script type=\”text/javascript\”>parent.KindInsertImage(’” + imgUrl + “‘,’” + imgWidth + “‘,’” + imgHeigth + “‘,’” + imgBorder + “‘,’” + imgTitle + “‘,’” + imgAlign + “‘,’” + imgHspace + “‘,’” + imgVspace + “‘)</script>”;
html += “</body>”;
html += “</html>”;
Response.Write(html);
}
}
else
{
Response.Write(”<script>alert(’请选择要上传的文件!此次上传失败’);</script>”);
}
}
private void Alert(string msg)
{
string altHtml = “<html>”;
altHtml += “<script type=\”text/javascript\”>alert(\”" + msg + “\”);parent.KindDisableMenu();parent.KindReloadIframe();</script>”;
Response.Write(altHtml);
}
}
}
另一个
using System;
using System.Globalization;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class Jscript_KindEditor_upload_cgi_upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//文件保存目录路径
string SavePath = "/Upload_Images/";
//文件保存目录URL
string SaveUrl = "/Upload_Images/";
//上传图片类型
string[] ExtStr=new string[4];
ExtStr[0] = ".gif";
ExtStr[1] = ".jpg";
ExtStr[2] = ".png";
ExtStr[3] = ".bmp";
//图片的最大大小
int MaxSize = 100000;
//错误提示
string[] MsgStr = new string[3];
MsgStr[0] = "上传文件大小超过限制.";
MsgStr[1] = "上传文件扩展名是不允许的扩展名.";
MsgStr[2] = "上传文件失败\\n请重试.";
string imgWidth = Request.Form["imgWidth"];
string imgHeight = Request.Form["imgHeight"];
string imgBorder = Request.Form["imgBorder"];
string imgTitle = Request.Form["imgTitle"];
string imgAlign = Request.Form["imgAlign"];
string imgHspace = Request.Form["imgHspace"];
string imgVspace = Request.Form["imgVspace"];
HttpPostedFile imgFile = HttpContext.Current.Request.Files["imgFile"];
//获得文件名
string FileName = System.IO.Path.GetFileName(imgFile.FileName);
if (FileName != "")
{
if (imgFile.ContentLength > MaxSize)
{
Alert(MsgStr[0]);
}
else
{
string fileExtension = System.IO.Path.GetExtension(FileName).ToLower();
if (CheckExt(ExtStr, fileExtension))
{
//重新为文件命名,时间毫秒部分+扩展名
string imgReName = "" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff", DateTimeFormatInfo.InvariantInfo) + "" + fileExtension;
//文件夹名
string imgFolderName=DateTime.Now.ToString("yyyyMMdd",DateTimeFormatInfo.InvariantInfo);
try
{
if (!System.IO.Directory.Exists(@Server.MapPath("" + SavePath + "" +imgFolderName + "")))
{
//生成文件完整目录
System.IO.Directory.CreateDirectory(@Server.MapPath("" + SavePath + "" +imgFolderName + ""));
}
imgFile.SaveAs(@Server.MapPath("" + SavePath + "" + imgFolderName + "/")+imgReName);
}
catch
{
Alert(MsgStr[2]);
}
string imgUrl = SaveUrl + imgFolderName + "/" + imgReName;
ReturnImg(imgUrl, imgWidth, imgHeight, imgBorder, imgTitle, imgAlign, imgHspace, imgVspace);
}
else
{
Alert(MsgStr[1]);
}
}
}
}
/// <summary>
/// 提示关闭层
/// </summary>
/// <param name="MsgStr"></param>
private void Alert(string MsgStr)
{
Response.Write("<html>");
Response.Write("<head>");
Response.Write("<title>error</title>");
Response.Write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
Response.Write("</head>");
Response.Write("<body>");
Response.Write("<script type=\"text/javascript\">alert(\"" + MsgStr + "\");parent.KindDisableMenu();parent.KindReloadIframe();</script>");
Response.Write("</body>");
Response.Write("</html>");
}
/// <summary>
/// 检测文件类型
/// </summary>
/// <param name="ExtStr"></param>
/// <param name="fileExt"></param>
/// <returns></returns>
private bool CheckExt(string[] ExtStr,string fileExt)
{
for (int i = 0; i < ExtStr.Length; i++)
{
if (ExtStr[i] != fileExt)
{
return true;
}
}
return false;
}
/// <summary>
/// 返回图片
/// </summary>
/// <param name="FileUrl"></param>
/// <param name="FileWidth"></param>
/// <param name="FileHeight"></param>
/// <param name="FileBorder"></param>
/// <param name="FileTitle"></param>
/// <param name="FileAlign"></param>
/// <param name="FileHspace"></param>
/// <param name="FileVspace"></param>
private void ReturnImg( string FileUrl,string FileWidth,string FileHeight,string FileBorder,string FileTitle,string FileAlign,string FileHspace,string FileVspace)
{
Response.Write("<html>");
Response.Write("<head>");
Response.Write("<title>上传成功</title>");
Response.Write("<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">");
Response.Write("</head>");
Response.Write("<body>");
Response.Write("<script type=\"text/javascript\">parent.KindInsertImage(\"" + FileUrl +"\",\"" + FileWidth + "\",\"" + FileHeight + "\",\"" + FileBorder + "\",\"" + FileTitle + "\",\"" + FileAlign + "\",\"" + FileHspace + "\",\"" + FileVspace + "\");</script>");
Response.Write("</body>");
Response.Write("</html>");
}
}
以上内容为网上转载,本人没测,等有空时再测后更改