如何用fckeditor编辑器上传图片、flash、视频到moss的图片库中
FCKEditor编辑器是一款功能齐全,性能出色的编辑器。
FCKEditor由于其开放源码,自定义非常强大,因此应用范围非常广泛,许多著名论坛的编辑器都是基于此编辑器更改的。
MOSS中的RTF编辑器小巧精悍,但功能齐全性上略有不及,很难适应挑涤的企业用户的需求。因此,在信息发布等模块中,
通常采用其它的第三方编辑器。如果采用其它编辑器,它与MOSS的楔合度将是一个不得不考虑的问题。楔合度越高,在用户
体验,后期维护上占有比较大的优势。
这里我将讲解更改FCKEditor的上传图片等文件的方式提高楔合度的一个案例。
FCKEditor默认情况下,图片等文件是上传到服务器文件系统中的一个文件夹内的。这种设计机置在其它的项目中,可能不会有什么问题,但如果在MOSS项目中,将是一个不得不正视的问题。
第一、MOSS备份需要额外考虑该文件夹的备份。
第二、在NLB环境下,文件上传将会随机上传到NBL中的某一台前端上,这是一个非常严重的缺陷,用户访问时,可能造成文件访问不到。
因此,如果FCKEditor要应用到MOSS中,将不得不更改这种上传机置。试想,如果能够将图片等文件上传到MOSS中的图片库或文档库上,那么一切将迎刃而解了。
具体实现如下:
一、打开FCKeditor.Net_2.6.3工程。(网上可以下载,FCKEditor支持.net的源码)
二、FileWorkerBase.cs
更改FileUpload方法。增加图片压缩方法CreateThumbnail.
代码如下:

 protected void FileUpload( string resourceType, string currentFolder, bool isQuickUpload )
protected void FileUpload( string resourceType, string currentFolder, bool isQuickUpload ) {
        { HttpPostedFile oFile = Request.Files[ "NewFile" ];
            HttpPostedFile oFile = Request.Files[ "NewFile" ];
 string sFileName = "";
            string sFileName = "";
 if ( oFile == null )
            if ( oFile == null ) {
            { this.SendFileUploadResponse( 202, isQuickUpload );
                this.SendFileUploadResponse( 202, isQuickUpload ); return;
                return; }
            }
 // Map the virtual path to the local server path.
            // Map the virtual path to the local server path. //string sServerDir = this.ServerMapFolder( resourceType, currentFolder, isQuickUpload );
            //string sServerDir = this.ServerMapFolder( resourceType, currentFolder, isQuickUpload );
 // Get the uploaded file name.
            // Get the uploaded file name. sFileName = System.IO.Path.GetFileName( oFile.FileName );
            sFileName = System.IO.Path.GetFileName( oFile.FileName ); sFileName = this.SanitizeFileName( sFileName );
            sFileName = this.SanitizeFileName( sFileName );
 string sExtension = System.IO.Path.GetExtension( oFile.FileName );
            string sExtension = System.IO.Path.GetExtension( oFile.FileName ); sExtension = sExtension.TrimStart( '.' );
            sExtension = sExtension.TrimStart( '.' );
 if ( !this.Config.TypeConfig[ resourceType ].CheckIsAllowedExtension( sExtension ) )
            if ( !this.Config.TypeConfig[ resourceType ].CheckIsAllowedExtension( sExtension ) ) {
            { this.SendFileUploadResponse( 202, isQuickUpload );
                this.SendFileUploadResponse( 202, isQuickUpload ); return;
                return; }
            }
 if ( this.Config.CheckIsNonHtmlExtension( sExtension ) && !this.CheckNonHtmlFile( oFile ) )
            if ( this.Config.CheckIsNonHtmlExtension( sExtension ) && !this.CheckNonHtmlFile( oFile ) ) {
            { this.SendFileUploadResponse( 202, isQuickUpload );
                this.SendFileUploadResponse( 202, isQuickUpload ); return;
                return; }
            }
 int iErrorNumber = 0;
            int iErrorNumber = 0; //int iCounter = 0;
            //int iCounter = 0;
 //文件上传到新闻图片库中
            //文件上传到新闻图片库中 string strFileUrl = string.Empty;
            string strFileUrl = string.Empty; string strURL = "http://"+this.Page.Request.Url.Authority;
            string strURL = "http://"+this.Page.Request.Url.Authority; SPSecurity.RunWithElevatedPrivileges(delegate
            SPSecurity.RunWithElevatedPrivileges(delegate {
            { //创建指定站点集
                //创建指定站点集 using (SPSite site = new SPSite(strURL))
                using (SPSite site = new SPSite(strURL)) {
                { //打开网站
                    //打开网站 using (SPWeb web = site.OpenWeb())
                    using (SPWeb web = site.OpenWeb()) {
                    { string strUserName = SPContext.Current.Web.CurrentUser.LoginName;
                        string strUserName = SPContext.Current.Web.CurrentUser.LoginName; strUserName = strUserName.Substring(strUserName.LastIndexOf("\\") + 1);
                        strUserName = strUserName.Substring(strUserName.LastIndexOf("\\") + 1);
 SPPictureLibrary plb = (SPPictureLibrary)web.Lists["新闻图片库"];
                        SPPictureLibrary plb = (SPPictureLibrary)web.Lists["新闻图片库"];
 oFile.InputStream.Position = 0;
                        oFile.InputStream.Position = 0; MemoryStream fStream = new MemoryStream();
                        MemoryStream fStream = new MemoryStream();
 if (sExtension.ToLower().Equals("jpg") ||
                        if (sExtension.ToLower().Equals("jpg") || sExtension.ToLower().Equals("gif") ||
                            sExtension.ToLower().Equals("gif") || sExtension.ToLower().Equals("jpeg") ||
                            sExtension.ToLower().Equals("jpeg") || sExtension.ToLower().Equals("png") ||
                            sExtension.ToLower().Equals("png") || sExtension.ToLower().Equals("bmp"))
                            sExtension.ToLower().Equals("bmp")) {
                        { 图像压缩开始
                            图像压缩开始 myThumbnail.Dispose();
                            myThumbnail.Dispose(); myBitmap.Dispose();
                            myBitmap.Dispose(); }
                        } else
                        else {
                        { fStream = (MemoryStream)oFile.InputStream;
                            fStream = (MemoryStream)oFile.InputStream; }
                        } fStream.Position = 0;
                        fStream.Position = 0; int intLength = (int)fStream.Length;
                        int intLength = (int)fStream.Length; byte[] contents = new byte[intLength];
                        byte[] contents = new byte[intLength]; fStream.Read(contents, 0, intLength);
                        fStream.Read(contents, 0, intLength); //fStream.Read(contents, 0, (int)fStream.Length);
                        //fStream.Read(contents, 0, (int)fStream.Length); 
                        strFileUrl = "http://" + this.Page.Request.Url.Authority + plb.DefaultViewUrl.Substring(0, plb.DefaultViewUrl.LastIndexOf("Forms/"));
                        strFileUrl = "http://" + this.Page.Request.Url.Authority + plb.DefaultViewUrl.Substring(0, plb.DefaultViewUrl.LastIndexOf("Forms/")); 
                         web.AllowUnsafeUpdates = true;
                        web.AllowUnsafeUpdates = true;
 sFileName = DateTime.Now.Year.ToString() +
                        sFileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() +
                        DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                        DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() +
                        DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() +
                        DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() +
                        DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + "." + sExtension;
                        DateTime.Now.Millisecond.ToString() + "." + sExtension; strFileUrl += sFileName;
                        strFileUrl += sFileName; SPFile spfile = web.Files.Add(strFileUrl, contents);
                        SPFile spfile = web.Files.Add(strFileUrl, contents); 
                                                 fStream.Dispose();
                        fStream.Dispose(); 
                         }
                    } }
                } });
            });
 this.SendFileUploadResponse(0, true, strFileUrl, sFileName);
            this.SendFileUploadResponse(0, true, strFileUrl, sFileName); }
        }
 /// <summary>
        /// <summary> /// A better alternative to Image.GetThumbnail. Higher quality but slightly slower
        /// A better alternative to Image.GetThumbnail. Higher quality but slightly slower /// </summary>
        /// </summary> /// <param name="source"></param>
        /// <param name="source"></param> /// <param name="thumbWi"></param>
        /// <param name="thumbWi"></param> /// <param name="thumbHi"></param>
        /// <param name="thumbHi"></param> /// <returns></returns>
        /// <returns></returns> public static Bitmap CreateThumbnail(Bitmap source, int thumbWi, int thumbHi, bool maintainAspect)
        public static Bitmap CreateThumbnail(Bitmap source, int thumbWi, int thumbHi, bool maintainAspect) {
        { // return the source image if it's smaller than the designated thumbnail
            // return the source image if it's smaller than the designated thumbnail if (source.Width < thumbWi && source.Height < thumbHi) return source;
            if (source.Width < thumbWi && source.Height < thumbHi) return source;
 System.Drawing.Bitmap ret = null;
            System.Drawing.Bitmap ret = null; try
            try {
            { int wi, hi;
                int wi, hi;
 wi = thumbWi;
                wi = thumbWi; hi = thumbHi;
                hi = thumbHi;
 if (maintainAspect)
                if (maintainAspect) {
                { // maintain the aspect ratio despite the thumbnail size parameters
                    // maintain the aspect ratio despite the thumbnail size parameters if (source.Width > source.Height)
                    if (source.Width > source.Height) {
                    { wi = thumbWi;
                        wi = thumbWi; hi = (int)(source.Height * ((decimal)thumbWi / source.Width));
                        hi = (int)(source.Height * ((decimal)thumbWi / source.Width)); }
                    } else
                    else {
                    { hi = thumbHi;
                        hi = thumbHi; wi = (int)(source.Width * ((decimal)thumbHi / source.Height));
                        wi = (int)(source.Width * ((decimal)thumbHi / source.Height)); }
                    } }
                }
 // original code that creates lousy thumbnails
                // original code that creates lousy thumbnails // System.Drawing.Image ret = source.GetThumbnailImage(wi,hi,null,IntPtr.Zero);
                // System.Drawing.Image ret = source.GetThumbnailImage(wi,hi,null,IntPtr.Zero); ret = new Bitmap(wi, hi);
                ret = new Bitmap(wi, hi); using (Graphics g = Graphics.FromImage(ret))
                using (Graphics g = Graphics.FromImage(ret)) {
                { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.FillRectangle(Brushes.White, 0, 0, wi, hi);
                    g.FillRectangle(Brushes.White, 0, 0, wi, hi); g.DrawImage(source, 0, 0, wi, hi);
                    g.DrawImage(source, 0, 0, wi, hi); }
                } }
            } catch
            catch {
            { ret = null;
                ret = null; }
            }
 return ret;
            return ret; }
        } 
3、编译,重新生成DLL。
该DLL支持将图片及其它上传文件上传到MOSS的新闻图片库中,同时会对上传的图片进行压缩处理。
 
                    
                     
                    
                 
                    
                 
 
        
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号