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);    //如果出錯,沒這一句的話,就沒有報錯了,只能看到一直要求你等待的狀態
                    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章