富文字編輯器ckeditor的上傳圖片配置
阿新 • • 發佈:2019-02-16
以前在工作中用過富文字編輯器,因為以前沒有用過這種東西,所以在使用的時候遇到了一些問題,就是儲存的時候,文字可以正常儲存,但是圖片無法儲存。後來經過配置解決了這個問題,對於新手來說,這個配置可能有點複雜,所以將它記錄下來。
首先新建一個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";
這樣圖片就能正常儲存了,資料庫中儲存圖片的路徑就行。