C# MVC 使用 CKEditor图片上传 提示“不正确的服务器响应”
生活随笔
收集整理的這篇文章主要介紹了
C# MVC 使用 CKEditor图片上传 提示“不正确的服务器响应”
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
重點:看一下你使用的CKEditor版本
過程:
后臺需要一款富文本編輯器。經過挑選后,最后選擇了FCKEditor 的升級版 CKEditor 。在官網下載了4.10.1版本。
經過一番配置后,富文本可以正常顯示。在上傳圖片的時候,出現了問題。一直提示我“不正確的服務器響應”。經過一番搜索發現配置和網上給出的配置都是一樣的,卻總還是錯誤。
?
后來發現一篇說新版本的CKEditor上傳圖片的返回值修改了。經過一番摸索,終于解決問題。
上圖:
?
原來之前的版本使用的通過 script 控制的tab跳轉并填入圖片地址的方式新版本已經棄用,改用新的Json 的方式傳遞。下面貼上配置和后端代碼:
?
CKEditor config.js配置
1 //上傳圖片的方法 2 config.filebrowserImageUploadUrl = "/Home/Upload"; 3 4 //圖片默認顯示文字為空 5 config.image_previewText = ' '; 6 7 //設置語言 8 config.language = 'zh-cn'; 9 10 // 解決CKEditor圖片寬度自適應的問題 p img { width: auto; height: auto; max - width: 100 %;} 11 config.disallowedContent = 'img{width,height};img[width,height]';?
后端Upload方法?
[HttpPost]public JsonResult Upload(HttpPostedFileBase upload){string savePath = "/upload/";string dirPath = Server.MapPath(savePath);//如果目錄不存在則創建目錄if (!Directory.Exists(dirPath))Directory.CreateDirectory(dirPath);//獲取圖片文件名及擴展名var fileName = Path.GetFileName(upload.FileName);string fileExt = Path.GetExtension(fileName).ToLower();//用時間來生成新文件名并保存string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;upload.SaveAs(dirPath + "/" + newFileName);//上傳成功后,我們還需要返回Json格式的響應return Json(new{uploaded = 1,fileName = newFileName,url = savePath + newFileName});}?
前端調用
//引入js文件<script src="~/Content/ckeditor/ckeditor.js"></script> <script src="~/Content/ckeditor/config.js"></script>
//ckditor容器 @Html.TextAreaFor(model => model.ContentInfo, new { @class = "ckeditor" })
?
轉載于:https://www.cnblogs.com/leoxuan/p/9743648.html
總結
以上是生活随笔為你收集整理的C# MVC 使用 CKEditor图片上传 提示“不正确的服务器响应”的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IDEA系列(十)--新建一个项目后之前
- 下一篇: 【Linux】Linux与Windows