xheditor编辑器跟ASP.NET一起使用
现在很多xheditor都是跟PHP、JSP使用,ASP.NET不算多,我这边就写个教程
这个编辑器简单实用,所以就成为我的理想首选。
好了,废话不多说,写教程。
先贴张效果图
1、首先去官网把该编辑器下载到自己的项目中,然后引用
<script type="text/javascript" src="xheditor/xheditor-1.1.9-zh-cn.min.js"></script>
2、然后在需要使用编辑器的地方加入这个JS。
<script type="text/javascript"> $(document).ready(function () { $('#<%=Article_Content.ClientID %>').xheditor({ tools: 'full', skin: 'default', showBlocktag: true, internalScript: false, internalStyle: false, width: 600, height: 400, forcePtag: true, upImgUrl: "/Cms/UploadHandler.ashx", upImgExt: "jpg,jpeg,gif,png" }); }); </script>
Article_Content这个控件我使用的是textarea,然后加的 runat="server"。
这样就能看到编辑器的样子了,不过只是这样还不够,图片和其他的flash之类还不能上传,所以我们需要添加上传的处理程序。
3、在第二步中的JS里面, upImgUrl: "/Cms/UploadHandler.ashx",这个ashx就是用来上传文件的,我把代码贴出来。
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Web.Cms { /// <summary> /// UploadHandler 的摘要说明 /// </summary> public class UploadHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Write(upLoadFile(context, "filedata")); } string upLoadFile(HttpContext context, string inputname) { string immediate; string attachdir; int maxattachsize; string upext; immediate = context.Request.QueryString["immediate"]; attachdir = "/UploadFile/"; // 上传文件保存路径,结尾不要带/ maxattachsize = 2097152; // 最大上传大小,默认是2M upext = "txt,rar,zip,jpg,jpeg,gif,png,swf,wmv,avi,wma,mp3,mid"; // 上传扩展名 string err, msg; err = ""; msg = ""; HttpFileCollection filecollection = context.Request.Files; // 只接收指定文件域的上传,如果需要同时接收多个文件,请通过循环方式接收 HttpPostedFile postedfile = filecollection.Get(inputname); if (postedfile == null) { err = "无数据提交"; } else { if (postedfile.ContentLength > maxattachsize) { err = "文件大小超过" + maxattachsize + "字节"; } else { string filename, extension, target; // 取上载文件后缀名 extension = GetFileExt(postedfile.FileName); if (("," + upext + ",").IndexOf("," + extension + ",") < 0) { err = "上传文件扩展名必需为:" + upext; } else { // 生成随机文件名 Random random = new Random(DateTime.Now.Millisecond); filename = DateTime.Now.ToString("yyyyMMddhhmmss") + random.Next(10000) + "." + extension; target = attachdir + filename; try { CreateFolder(context.Server.MapPath(attachdir)); postedfile.SaveAs(context.Server.MapPath(target)); msg = target; } catch (Exception ex) { err = ex.Message.ToString(); } // 立即模式判断 if (immediate == "1") msg = "!" + msg; } } } postedfile = null; filecollection = null; return "{err:'" + jsonString(err) + "',msg:'" + jsonString(msg) + "'}"; } string jsonString(string str) { str = str.Replace("\\", "\\\\"); str = str.Replace("/", "\\/"); str = str.Replace("'", "\\'"); return str; } string GetFileExt(string FullPath) { if (FullPath != "") { return FullPath.Substring(FullPath.LastIndexOf('.') + 1).ToLower(); } else { return ""; } } void CreateFolder(string FolderPath) { if (!System.IO.Directory.Exists(FolderPath)) { System.IO.Directory.CreateDirectory(FolderPath); } } public bool IsReusable { get { return false; } } } }
这样就可以正常使用了。