致此,“浏览服务器端图片或文件”部分就完成了。
上传图片或文件
上图:
因为这个弹框是CKEditZ http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcszhuam1xKOsztLDx9a70OjSqrSmwO3H68fzoaPPwsPmyse0psDtV0VCx+vH87XEQUNUSU9OKL/J0tTTw8jOus7T79HUtKbA7dCpx+vH86Oswt+8rcnPtPPM5c/gzawpo7oKPGJyPgoKPHByZSBjbGFzcz0="brush:java;">[HttpPost] public ActionResult FileUpload() { var ckEditorFuncNum = Request.QueryString["CKEditorFuncNum"]; var type = Request.QueryString["Type"]; var isImage = !string.IsNullOrEmpty(type) && type.Equals("Images", StringComparison.InvariantCultureIgnoreCase); var maxContentLength = isImage 512*1024 : 1024*1024; var file = Request.Files["upload"]; if (file == null) { return Content("No file has been chosen!"); } if (file.ContentLength > maxContentLength) { return Content("The image file size should be no bigger than 512KB! The document file size should be no bigger than 1024KB!"); } var urlpath = string.Empty; var datestamp = DateTime.Now.ToString("MMddyyyy"); var rootfolderpath = isImage "/Images/" : "/docs/"; var folderpath = Server.MapPath(rootfolderpath) + datestamp; if (file.ContentLength > 0) { var filename = Path.GetFileNameWithoutExtension(file.FileName); var fileextension = Path.GetExtension(file.FileName); var timestamp = DateTime.Now.ToString("MMddyyyyHHmmssfff"); var filepath = string.Format("{0}/{1}{2}{3}", folderpath, filename, timestamp, fileextension); urlpath = string.Format("{4}{0}/{1}{2}{3}", datestamp, filename, timestamp, fileextension, rootfolderpath); if (!Directory.Exists(folderpath)) { Directory.CreateDirectory(folderpath); } file.SaveAs(filepath); } return Content( string.Format( @"<script type=""text/java script"">window.parent.CKEDITOR.tools.callFunction({0}, ""{1}"");", ckEditorFuncNum, urlpath)); }
如果你不懂ASP.NET MVC,下面做一些简单的解释。[HttpPost]表明只处理POST请求,return Content("")是返回一个纯文本字符串,其它部分大同小异。唯一值得注意的是最后返回了一段java script脚本给CKEditor,它促使窗口跳转到Image Info选项卡并把图片URL传递过去,然后预览。如下图:
图片或文件上传部分到这里为止,下面介绍怎么配置CKEditor。
CKEditor配置
CKEditor里有一个配置文件config.js,我们只需要设置几个链接就可以了。代码如下:
/** * @license Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.html or http://ckeditor.com/license */ CKEDITOR.editorConfig = function (config) { config.extraPlugins = "autogrow,imagebrowser,filebrowser"; /*config autogrow*/ config.autoGrow_maxHeight = 400; /*config autogrow end*/ /*config imagebrowser*/ //config.imageBrowser_listUrl = ""; /*config imagebrowser end*/ /*config filebrowser*/ config.filebrowserImageBrowseUrl = '/SHTracker/CKEditor/Browse type=Images'; config.filebrowserImageUploadUrl = '/SHTracker/CKEditor/FileUpload type=Images'; config.filebrowserBrowseUrl = '/SHTracker/CKEditor/Browse type=docs'; config.filebrowserUploadUrl = '/SHTracker/CKEditor/FileUpload type=docs'; config.filebrowserImageWindowWidth = 640; config.filebrowserImageWindowHeight = 480; /*config filebrowser end*/ // Define