KindEditor图片上传功能ASP.NET的CGI实现

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>");
}
}

以上内容为网上转载,本人没测,等有空时再测后更改

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
KindEditor是一款基于JavaScript的富文本编辑器,它可以方便地集成到网站中,并支持图片上传功能。在Spring Boot中实现KindEditor图片上传可以按照以下步骤进行: 1. 在Spring Boot中添加以下依赖: ```xml <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> ``` 2. 创建一个Controller用于处理图片上传请求,代码如下: ```java @RestController @RequestMapping("/upload") public class FileUploadController { @PostMapping("/image") public String uploadImage(HttpServletRequest request) throws Exception { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile file = multipartRequest.getFile("imgFile"); String filename = file.getOriginalFilename(); String suffix = filename.substring(filename.lastIndexOf(".") + 1); String newFilename = UUID.randomUUID().toString() + "." + suffix; String savePath = "D:/upload/images/" + newFilename; // 上传文件保存路径,根据实际情况修改 File destFile = new File(savePath); if (!destFile.getParentFile().exists()) { destFile.getParentFile().mkdirs(); } file.transferTo(destFile); String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/upload/images/" + newFilename; // 图片访问URL,根据实际情况修改 return "{\"error\":0,\"url\":\"" + url + "\"}"; } } ``` 3. 在HTML页面中集成KindEditor,并设置图片上传的请求地址,代码如下: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>KindEditor图片上传示例</title> <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/kindeditor/4.1.11/kindeditor-all.min.css" /> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/kindeditor/4.1.11/kindeditor-all.min.js"></script> </head> <body> <textarea id="editor"></textarea> <script> KindEditor.ready(function(K) { K.create('#editor', { uploadJson: '/upload/image', allowFileManager: false }); }); </script> </body> </html> ``` 4. 启动Spring Boot应用,访问HTML页面即可进行图片上传。上传的图片会保存在指定的路径中,并返回图片的访问URL给KindEditor进行显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值