ASP.NET環境下整合CKEditor與CKEditor實現檔案上傳

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   使用   sp   

1、從http://ckeditor.com網站上下載ckeditor_aspnet_3.6.4與ckfinder_aspnet_2.4;

2、解壓下載的檔案ckeditor_aspnet_3.6.4.zip,得到

解壓下載的檔案ckfinder_aspnet_2.4.zip,得到

3、將ckeditor_aspnet_3.6.4\_Samples\bin檔案夾下的CKEditor.NET.dll和CKEditor.NET.pdb,以及ckfinder_aspnet_2.4\ckfinder\bin\Debug檔案夾下的CKFinder.dll和CKFinder.pdb檔案複製到項目bin檔案夾中;

4、將CKEditor和CKFinder檔案夾複製到項目中

 

5、修改項目設定檔web.config和Ckeditor\config.js、Ckfinder\config.ascx;

(1)web.config檔案中在<system.web></system.web>之間加入:

    <pages>

        <controls>

            <add tagPrefix="CKEditor" assembly="CKEditor.NET" namespace="CKEditor.NET"/>

        </controls>

    </pages>

(2)Ckeditor\config.js整合指定上傳檔案瀏覽服務、上傳圖片、上傳FLASH等調用的CKFinder地址,修改如下:

CKEDITOR.editorConfig = function( config )

{

    // Define changes to default configuration here. For example:

    // config.language = ‘fr‘;

    // config.uiColor = ‘#AADC6E‘;

    config.language = ‘zh-cn‘;

    config.filebrowserBrowseUrl = ‘/ckfinder/ckfinder.html‘; //上傳檔案時瀏覽服務檔案夾

    config.filebrowserImageBrowseUrl = ‘/ckfinder/ckfinder.html?Type=Images‘; //上傳圖片時瀏覽服務檔案夾

    config.filebrowserFlashBrowseUrl = ‘/ckfinder/ckfinder.html?Type=Flash‘;  //上傳Flash時瀏覽服務檔案夾

    config.filebrowserUploadUrl = ‘/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Files‘; //上傳檔案按鈕(標籤)

    config.filebrowserImageUploadUrl = ‘/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images‘; //上傳圖片按鈕(標籤)

    config.filebrowserFlashUploadUrl = ‘/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Flash‘; //上傳Flash按鈕(標籤)

};

 

(3)Ckfinder\config.ascx指定上傳檔案時是否需要授權,預設返回false,修改為true。(注意:在正式項目中不可直接返回true,而要驗證使用者登入成功後才可返回!)

public override bool CheckAuthentication()

    {

        // WARNING : DO NOT simply return "true". By doing so, you are allowing

        // "anyone" to upload and list the files in your server. You must implement

        // some kind of session validation here. Even something very simple as...

        //

        //      return ( Session[ "IsAuthorized" ] != null && (bool)Session[ "IsAuthorized" ] == true );

        //

        // ... where Session[ "IsAuthorized" ] is set to "true" as soon as the

        // user logs on your system.

        return true ;

    }

6、在項目中建立一個editor.aspx,源碼如下:

<body>

    <form id="form1" runat="server">

    <div>

    <CKEditor:CKEditorControl ID="CKEditor1" runat="server" Height="200" BasePath="~/ckeditor">

        &lt;p&gt;This is some &lt;strong&gt;sample text&lt;/strong&gt;. You are using &lt;a href="http://ckeditor.com/"&gt;CKEditor&lt;/a&gt;.&lt;/p&gt;

        </CKEditor:CKEditorControl>

    </div>

    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

    <br />

    <asp:Literal ID="Literal1" runat="server"></asp:Literal>

    </form>

</body>

在editor.aspx.cs檔案中輸入:

protected void Button1_Click(object sender, EventArgs e)

    {

        Literal1.Text = CKEditor1.Text;

}

7、修改CKFinder使其動態命名上傳的檔案,重建CKFinder.dll檔案

修改CKFinder的源碼。CKFinder內建有源碼,目錄:/CKFinde/_source
在VS裡建立一個 現在的項目 指向CKFinde/_source/CKFinder.Net.sln
(1) 開啟/Settings/ConfigFile.cs檔案
定位27行,添加一個屬性:public bool RandomReName; //隨機重新命名
定位63行,給剛才的屬性賦值:RandomReName = true; //預設值為true
儲存關閉檔案
(2) 開啟/Connector/Config.cs檔案
定位61行,添加一個屬性:
        public bool RandomReName
        {
            get { return Settings.ConfigFile.Current.RandomReName; }
        }
儲存關閉檔案
(3) 開啟/Connector/CommandHandlers/FileUploadCommandHandler.cs檔案
定位84行,添加一句判斷代碼:
                        if ( Config.Current.RandomReName)  //使用隨機名
                            sFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "." + sExtension;

儲存關閉檔案,注意:這裡會跳出一個錯誤,sExtension未定義,尋找一下這個變數的定義,然後放在它後面就行了。

(4) 重建項目,把/ckfinder/_source/bin/Debug/CKFinder.dll覆蓋/ckfinder/bin/Release/CKFinder.dll或者網站項目去掉之前加入的引用再重新從CKFinder.Net項目裡的Dll

 

 

 

 

ASP.NET環境下整合CKEditor與CKEditor實現檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.