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