百度ueditor編輯器在Asp.Net中使用

來源:互聯網
上載者:User

<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest="false" CodeBehind="ueditor_1_2.aspx.cs" Inherits="UEditor.ueditor_1_2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>百度ueditor編輯器在Asp.Net中使用</title>

    <script src="ueditor1.2.0/editor_config.js" type="text/javascript"></script>

    <script src="ueditor1.2.0/editor_all.js" type="text/javascript"></script>

    <link href="ueditor1.2.0/themes/default/ueditor.css" rel="stylesheet" type="text/css" />

</head>

<body>

    <form id="myForm" runat="server" method="post">

    <div>

     <!--<div id="myEditor"></div>-->

      <!--賦初值-->

    <script type="text/plain" id="myEditor">初始內容</script>

    <!--隱藏控制項,由Asp.Net賦值(臨時存放)-->

    <div id="HiddenDiv" runat="server" style="display:none;"></div>

    <script type="text/javascript">

             var URL = "./ueditor/";    //這裡你可以配置成ueditor目錄在您網站的相對路徑或者絕對路徑

              var editor = new baidu.editor.ui.Editor();

              

              editor.render("myEditor");

              editor.setHeight(100); //設定編輯器的高度

              //editor.setContent("為編輯器設定初值");

              editor.setContent(document.getElementById("<%=this.HiddenDiv.ClientID%>").innerHTML);//從隱藏控制項中取出臨時資料

    </script>

    </div>

    <asp:Button ID="Button1" runat="server" Text="伺服器控制項" />

    <input id="Button2" onclick="submitData()" type="button" value="HTML控制項" /></form>

    <p>

        &nbsp;</p>

    <p>

        &nbsp;</p>

</body>

</html>

<script type="text/javascript">

    function submitData() {

        if (editor.hasContents()) {  //提交條件滿足時提交內容

            editor.sync();           //此處的editor是頁面執行個體化出來的編輯器對象

            var v = editor.getContent();

            alert(v);

            document.getElementById('myForm').submit();

        }

    }

</script>

//------------------------------.cs部分--------------------------

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace UEditor

{

    public partial class ueditor_1_2 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            if (Request.Params["editorValue"] != null)

            {

                string content = Request.Params["editorValue"];//擷取編輯器的內容

                this.HiddenDiv.InnerHtml = content;//保證回傳不丟值

            }

            else

            {

                this.HiddenDiv.InnerHtml = "伺服器端載入的初始值";

            }

        }

    }

}

//------------------------------上傳圖片配置-----------------------------

修改設定檔editor_config.js中的

imagePath屬性可以為空白,更換上傳檔案路徑需修改dialogs/image/image.html檔案,如圖:


//--------------------------FilesUpload.ashx檔案代碼-------------------------------

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

namespace UEditor.Ajax

{

    /// <summary>

    /// FilesUpload 的摘要說明

    /// </summary>

    public class FilesUpload : IHttpHandler

    {

 

        public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/plain";

            ///context.Response.Write("Hello World");

            string state = "SUCCESS";

           HttpFileCollection files=context.Request.Files;

           if (files.Count > 0)

           {

               string rootPath = context.Request.MapPath("~/") + "UploadFiles/";

               string url = String.Empty;

               for (int i = 0; i < files.Count;i++ )

               {

                   try

                   {

                       files[i].SaveAs(rootPath + files[i].FileName);

                       //url = "/UploadFiles/" + files[i].FileName;

                       url = files[i].FileName;//在editor_config.js檔案中配置有根目錄路徑

                   }

                   catch

                   {

                       state = "ERROR";

                   }

               }

               HttpContext.Current.Response.Write("{'url':'" + url + "','title':'" + context.Request.Params["pictitle"] + "','state':'" + state + "'}");

           }

        }

 

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.