富文本编辑器ckeditor的上传图片配置

以前在工作中用过富文本编辑器,因为以前没有用过这种东西,所以在使用的时候遇到了一些问题,就是保存的时候,文本可以正常保存,但是图片无法保存。后来经过配置解决了这个问题,对于新手来说,这个配置可能有点复杂,所以将它记录下来。

首先新建一个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";

这样图片就能正常保存了,数据库中保存图片的路径就行。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值