以前在工作中用过富文本编辑器,因为以前没有用过这种东西,所以在使用的时候遇到了一些问题,就是保存的时候,文本可以正常保存,但是图片无法保存。后来经过配置解决了这个问题,对于新手来说,这个配置可能有点复杂,所以将它记录下来。
首先新建一个ashx文件,代码如下
public class UploadweixinImgHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
String callback = context.Request.QueryString["CKEditorFuncNum"].ToString();
///'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
for (int iFile = 0; iFile < files.Count; iFile++)
{
// ///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName; //, fileExtension
fileName = System.IO.Path.GetFileName(postedFile.FileName);
string fileContentType = postedFile.ContentType.ToString();
if (fileContentType == "image/bmp" || fileContentType == "image/gif" ||
fileContentType == "image/png" || fileContentType == "image/x-png" || fileContentType == "image/jpeg"
|| fileContentType == "image/pjpeg")
{
if (postedFile.ContentLength <= 2097152)
{
string filepath = postedFile.FileName;
//string filepath = FileUpload1.FileName; //得到上传的文件名20022775_m.jpg
string serverpath = context.Server.MapPath("~/WeiXinImg/") + fileName;//取得文件在服务器上保存的位置F:\work\ys\ying_app\ying\WeiXinImg\转账@2x.png
postedFile.SaveAs(serverpath);//上传图片到服务器指定地址
string imageurl = @"/WeiXinImg/" + fileName;//我是将测试时的本地地址+放置图像的文件夹+图片名称作为返回的URL,
// 返回"图像"选项卡并显示图片
context.Response.Write("<script type=\"text/javascript\">");
context.Response.Write("window.parent.CKEDITOR.tools.callFunction(" + callback
+ ",'" + imageurl + "','')");
context.Response.Write("</script>");
}
else
{
context.Response.Write("<script>alert('上传文件不能大于2M!')</script>");
}
}
else
{
context.Response.Write("<script>alert('只支持BMP、GIF、JPG、PNG格式的图片!')</script>");
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
然后在富文本编辑器的 config.js 文件中 加上这个ashx文件 :
config.filebrowserImageUploadUrl = "../UploadweixinImgHandler.ashx";
这样图片就能正常保存了,数据库中保存图片的路径就行。