FCKeditor 上传自动重命名、按月创建文件夹和基本操作

现在做的项目,用到了编辑器,而FCK都出到了2.6.3所以就把他改成自己想要的,发现跟原来的不一样了,还要重新找,现在就把过程发给大家分享一下吧!

 

一、自定义 FCKeditor 的 BasePath

BasePath 即FCKeditor在网站中的相对路径,默认值是 /fckeditor/,最好在Web.config appSettings中对其进行配置:

< add  key ="FCKeditor:BasePath"  value ="/FCKeditor_2.6.3/" />

这样做有诸多优点:
  1. 开发环境与生产环境不同,开发环境一般是http://localhost/xxx.com/这种情况下FCKeditor就得放在一个虚拟目录http://localhost/fckeditor/中,若涉及多个网站的开发,而各网站的FCKeditor有差别时,这样显然不是最优;
    而且因为物理目录结构与逻辑目录结构不同,也会有发生错误的隐患;
    而如果采用Web.config的配置,就可以在开发环境采用不同的配置,FCKeditor的物理路径与生产环境保持一致;
  2. 当升级FCKeditor时,只需要将新版本的FCKeditor放在相应版本号的目录里,修改一下配置即可。这样可以解决因为静态资源的客户端缓存问题,不同用户出现不同的错误的问题;
  3. 可以直观地看到自己的FCKeditor的版本号。

 

二、配置文件上传的目录

FCKeditor的文件上传(如图片上传)目录可以通过Web.config appSettings进行配置,如:

< add  key ="FCKeditor:UserFilesPath"  value ="/UploadFile/FCKeditor/" />


也可以在 /FCKeditorBasePath/editor/filemanager/connectors/aspx/config.ascx 中进行配置,但我建议 FCKeditor 目录中的内容能不改就不改(fckconfig.js除外),这样日后升级可以放心地替换即可。

 

三、FCKeditor的安全性

在FCKeditor的2.3.2版本里,曾有一个漏洞,可以通过 /editor/filemanager/browser/default/connectors/aspx/connector.aspx 往服务器上传任意文件,我的网站就曾经中招。

2.6.3虽然暂未发现类似的问题,但一般情况下用不到的文件最好还是删除比较好:

  1. FCKeditor BasePath 根目录中除了保留:
    1. /editor
    2. /fckconfig.js
    3. /fckpackager.xml
    4. /fckstyles.xml
    5. /fcktemplates.xml
    6. /license.txt
    外,全部删除

  2. /editor/filemanager/connectors中除了保留:
    1. /aspx/config.ascx
    2. /aspx/upload.aspx
    3. /aspx/connector.aspx
    外,全部删除

  3. 删除 /editor/_source/

  4. /editor/filemanager/connectors/aspx/config.ascx 的 CheckAuthentication() 方法中,增加验证用户是否登录的逻辑

四、上传所有东西按年-月的格式存储

(他的两种上传模式是放在两个目录下的,快捷模式是放在UploadFiles下,而浏览模式是在此目录下创建对应的文件类型文件夹,我很不喜欢,所以就都改在一个目录下了!)

  在 /editor/filemanager/connectors/aspx/config.ascx文件中修改SetConfig()方法,改后的代码如下:

 

        TypeConfig[  " File "  ].AllowedExtensions             =   new   string [] {  " 7z " " aiff " " asf " " avi " " bmp " " csv " " doc " " fla " " flv " " gif " " gz " " gzip " " jpeg " " jpg " " mid " " mov " " mp3 " " mp4 " " mpc " " mpeg " " mpg " " ods " " odt " " pdf " " png " " ppt " " pxd " " qt " " ram " " rar " " rm " " rmi " " rmvb " " rtf " " sdc " " sitd " " swf " " sxc " " sxw " " tar " " tgz " " tif " " tiff " " txt " " vsd " " wav " " wma " " wmv " " xls " " xml " " zip "  };
        TypeConfig[ 
" File "  ].DeniedExtensions             =   new   string [] { };
        TypeConfig[ 
" File "  ].FilesPath                     =   " %UserFilesPath%file/ "   +  DateTime.Now.ToString( " yyyy-MM " +   " / " ;
        TypeConfig[ 
" File "  ].FilesAbsolutePath             =  ( UserFilesAbsolutePath  ==   ""   ?   ""  :  " %UserFilesAbsolutePath%file/ "   +  DateTime.Now.ToString( " yyyy-MM " +   " / "  );
        TypeConfig[ 
" File "  ].QuickUploadPath             =   " %UserFilesPath%file/ "   +  DateTime.Now.ToString( " yyyy-MM " +   " / " ;
        TypeConfig[ 
" File "  ].QuickUploadAbsolutePath     =  ( UserFilesAbsolutePath  ==   ""   ?   ""  :  " %UserFilesAbsolutePath%file/ "   +  DateTime.Now.ToString( " yyyy-MM " +   " / "  );

        TypeConfig[ 
" Image "  ].AllowedExtensions             =   new   string [] {  " bmp " " gif " " jpeg " " jpg " " png "  };
        TypeConfig[ 
" Image "  ].DeniedExtensions             =   new   string [] { };
        TypeConfig[ 
" Image "  ].FilesPath                     =   " %UserFilesPath%image/ "   +  DateTime.Now.ToString( " yyyy-MM " +   " / " ;
        TypeConfig[ 
" Image "  ].FilesAbsolutePath             =  ( UserFilesAbsolutePath  ==   ""   ?   ""  :  " %UserFilesAbsolutePath%image/ "   +  DateTime.Now.ToString( " yyyy-MM " +   " / "  );
        TypeConfig[ 
" Image "  ].QuickUploadPath             =   " %UserFilesPath%image/ "   +  DateTime.Now.ToString( " yyyy-MM " +   " / " ;
        TypeConfig[ 
" Image "  ].QuickUploadAbsolutePath     =  ( UserFilesAbsolutePath  ==   ""   ?   ""  :  " %UserFilesAbsolutePath%image/ "    +  DateTime.Now.ToString( " yyyy-MM " +   " / " );

        TypeConfig[ 
" Flash "  ].AllowedExtensions             =   new   string [] {  " swf " " flv "  };
        TypeConfig[ 
" Flash "  ].DeniedExtensions             =   new   string [] { };
        TypeConfig[ 
" Flash "  ].FilesPath                     =   " %UserFilesPath%flash/ "   +  DateTime.Now.ToString( " yyyy-MM " +   " / " ;
        TypeConfig[ 
" Flash "  ].FilesAbsolutePath             =  ( UserFilesAbsolutePath  ==   ""   ?   ""  :  " %UserFilesAbsolutePath%flash/ "   +  DateTime.Now.ToString( " yyyy-MM " +   " / "  );
        TypeConfig[ 
" Flash "  ].QuickUploadPath             =   " %UserFilesPath%flash/ "   +  DateTime.Now.ToString( " yyyy-MM " +   " / " ;
        TypeConfig[ 
" Flash "  ].QuickUploadAbsolutePath     =  ( UserFilesAbsolutePath  ==   ""   ?   ""  :  " %UserFilesAbsolutePath%flash/ "   +  DateTime.Now.ToString( " yyyy-MM " +   " / "  );

        TypeConfig[ 
" Media "  ].AllowedExtensions             =   new   string [] {  " aiff " " asf " " avi " " bmp " " fla " " flv " " gif " " jpeg " " jpg " " mid " " mov " " mp3 " " mp4 " " mpc " " mpeg " " mpg " " png " " qt " " ram " " rm " " rmi " " rmvb " " swf " " tif " " tiff " " wav " " wma " " wmv "  };
        TypeConfig[ 
" Media "  ].DeniedExtensions             =   new   string [] { };
        TypeConfig[ 
" Media "  ].FilesPath                     =   " %UserFilesPath%media/ "   +  DateTime.Now.ToString( " yyyy-MM " +   " / " ;
        TypeConfig[ 
" Media "  ].FilesAbsolutePath             =  ( UserFilesAbsolutePath  ==   ""   ?   ""  :  " %UserFilesAbsolutePath%media/ "   +  DateTime.Now.ToString( " yyyy-MM " +   " / "  );
        TypeConfig[ 
" Media "  ].QuickUploadPath             =   " %UserFilesPath%media/ "   +  DateTime.Now.ToString( " yyyy-MM " +   " / " ;
        TypeConfig[ 
" Media "  ].QuickUploadAbsolutePath     =  ( UserFilesAbsolutePath  ==   ""   ?   ""  :  " %UserFilesAbsolutePath%media/ "   +  DateTime.Now.ToString( " yyyy-MM " +   " / "  );

 

效果我就不发了,肯定行。

 

四、上传所有东西重命名

 

 FCKeditor 的文件上传默认是不改名的,本地的文件名是什么,上传后保留原来的文件名;如果存在同名文件,则会被自动在文件名后面加 (n) 来标识。

FCKeditor For ASP.NET 的上传部分被编译到 DLL 文件里面了,所以只能通过修改源代码,再重新编译后方能使用。

使用:FCKeditor.Net_2.6.3.zip,asp.net 2.0版

找到项目中的FileBrowser/FileWorkerBase.cs

 

 

             while  (  true  )
            
{
                
string sFilePath = System.IO.Path.Combine( sServerDir, sFileName );

                
if ( System.IO.File.Exists( sFilePath ) )
                
{
                    iCounter
++;
                    sFileName 
=
                        System.IO.Path.GetFileNameWithoutExtension( oFile.FileName ) 
+
                        
"(" + iCounter + ")." +
                        sExtension;

                    iErrorNumber 
= 201;
                }

                
else
                
{
                    oFile.SaveAs( sFilePath );
                    
break;
                }

            }

 

 

修改后的代码变成:

 

 

                 while  ( true )
                
{
                    sFileName 
= DateTime.Now.ToString("yyyymmddhhmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo) + "." + sExtension;//以时间命名文件

                    
string sFilePath = System.IO.Path.Combine(sServerDir, sFileName);

                    oFile.SaveAs(sFilePath);
                    
break;
                }

 

 

重新生成解决方案。在网站项目中删除旧的FredCK.FCKeditorV2.dll,再添加新的引用,就OK了。

 

四、上传图片带水印

 

在看代码之前先简单的介绍一下我的想法。我在自己的网站中建立了H2Blog.config文件,用于存放网站的一些配置信息,如水印的类型(文字型,图片型),是否需要加水印,文字型水印的文字内容等等和本文无关的重要配置信息。所以在如下带代码中,有一段是用来读取这些配置信息的。
在FileUpload方法中找到oFile.SaveAs( sFilePath );语句。在其后加入

 

 

                     try
                    
{
                        DataSet configds 
= new DataSet();   //新建一个数据集实例
                        configds.ReadXml(Server.MapPath("~/H2Blog.config"));   //获得配置信息
                        DataTable configdt = configds.Tables[0];    //将数据装入表中
                        if (configdt.Rows[0]["Watermarkstatus"].ToString() == "0")  //如果水印状态信息为默认加水印的话
                        {
                            Image img 
= Image.FromFile(sFilePath);  //获得上传的图片文件
                            if (configdt.Rows[0]["Watermarktype"].ToString() == "0")    //如果水印类型为文字型
                            {
                                Graphics g 
= Graphics.FromImage(img);
                                g.DrawImage(img, 
00, img.Width, img.Height);
                                Font f 
= new Font("黑体"20);
                                Brush b 
= new SolidBrush(Color.White);
                                
string addText = configdt.Rows[0]["Watermarktext"].ToString();  //文件型水印的文字内容
                                g.DrawString(addText, f, b, img.Width - 150, img.Height - 35);  //在图片上显示的坐标,这个我只能写死,不知道有没有更好的办法
                                g.Dispose();
                            }

                            
if (configdt.Rows[0]["Watermarktype"].ToString() == "1")    //如果水印类型为文字型
                            {
                                System.Drawing.Image copyImage 
= System.Drawing.Image.FromFile(Server.MapPath("~/Images/watermark/watermark.gif"));
                                Graphics g 
= Graphics.FromImage(img);
                                g.DrawImage(copyImage, 
new Rectangle(img.Width - copyImage.Width, img.Height - copyImage.Height, copyImage.Width, copyImage.Height), 00, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
                                g.Dispose();
                            }

                            sFileName 
= System.IO.Path.GetFileNameWithoutExtension(oFile.FileName); //获得源文件名
                            string newPath = DateTime.Now.ToString("yyyymmddhhmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo) + "." + sExtension; //以时间命名文件
                            sFileName = newPath;
                            newPath 
= System.IO.Path.Combine(sServerDir, newPath);  //组合地址
                            img.Save(newPath);  //保存加水印后图片
                            img.Dispose();
                            
if (File.Exists(sFilePath))
                            
{
                                File.Delete(sFilePath); 
//删除没上水印的老图
                            }

                        }

                    }

                    
catch
                    
{
                        
this.SendFileUploadResponse(808, isQuickUpload);    //如果出错,没这一句的话,就没有报错了,只能看到一直要求你等待的状态
                    }

你可能感兴趣的:(fckeditor)