php+tinymce粘貼word

最近公司作項目須要實現一個功能,在網頁富文本編輯器中實現粘貼Word圖文的功能。javascript

咱們在網站中使用的Web編輯器比較多,都是根據用戶需求來選擇的。目前尚未固定哪個編輯器php

有時候用的是UEditor,有時候用的CKEditor,KindEditor,TinyMCE。css

在網上查了不少資料,UEditor和其它的Web編輯器(富文本編輯器)在Chrome中能夠支持單張圖片粘貼。可是咱們的用戶須要處理的是Word中的圖片和文字,通常狀況下Word中的圖片可能有十幾張。有時候有幾十張。特別是用戶發一些教程或者使用說明類的文檔時圖片都是大幾十張的。 html

在網上找到說UEditor支持word粘貼,試了一下,只支持一張圖片的粘貼。多張圖片粘貼還須要用戶自已手動選擇。也就是說若是用戶粘貼的Word中包含20張圖片的話,那麼用戶就須要手動選擇20次,這種操做用戶是不可能接受的。前端

網上找了好久,大部分都有一些不成熟的問題,皇天不負有心人終於讓我找到了一個成熟的項目。java

1、前端引用代碼
jquery

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

<htmlxmlns="http://www.w3.org/1999/xhtml">瀏覽器

<head>安全

    <metahttp-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>編輯器完整版實例-1.2.6.0</title>

    <scripttype="text/javascript" src="ueditor.config.js" charset="utf-8"></script>

    <scripttype="text/javascript" src="ueditor.all.js" charset="utf-8"></script>

    <linktype="text/css" rel="Stylesheet" href="WordPaster/css/WordPaster.css"/>

    <linktype="text/css" rel="Stylesheet" href="WordPaster/js/skygqbox.css" />

    <scripttype="text/javascript" src="WordPaster/js/json2.min.js" charset="utf-8"></script>

    <scripttype="text/javascript" src="WordPaster/js/jquery-1.4.min.js" charset="utf-8"></script>

    <scripttype="text/javascript" src="WordPaster/js/WordPaster.js" charset="utf-8"></script>

    <scripttype="text/javascript" src="WordPaster/js/skygqbox.js" charset="utf-8"></script>

</head>

<body>

    <textareaname="後臺取值的key"id="myEditor">這裏寫你的初始化內容</textarea>

    <scripttype="text/javascript">

        var pasterMgr = new WordPasterManager();

    pasterMgr.Config["PostUrl"] = "http://localhost:81/WordPaster2/WordPasterUEditor1x/php/upload.php"

    pasterMgr.Load();//加載控件

        

          UE.getEditor('myEditor',{onready:function(){//建立一個編輯器實例

              pasterMgr.SetEditor(this);

          }});

    </script>

</body>

</html>


請求

文件上傳的默認請求是一個文件,做爲具備「upload」字段的表單數據。

響應:文件已成功上傳

當文件成功上傳時的JSON響應:

uploaded- 設置爲1。

fileName - 上傳文件的名稱。

url - 上傳文件的URL。

響應:文件沒法上傳

uploaded- 設置爲0。

error.message - 要顯示給用戶的錯誤消息。

2、粘貼word裏面的圖片路徑是fill://D 這種格式 我理解這種是非瀏覽器安全的 許多瀏覽器也不支持

目前項目是用了一種變通的方式:

先把word上傳到後臺 、poi解析、存儲圖片 、轉換html、替換圖片、放到富文本框裏顯示

(富文本顯示有個坑:沒找到直接給富文本賦值的方法 要先銷燬 記錄下

success : function(data) {

     $('#content').attr('value',data.imagePath);

     var editor = CKEDITOR.instances["content"]; //你的編輯器的"name"屬性的值

     if (editor) {

       editor.destroy(true);//銷燬編輯器

      }     

     CKEDITOR.replace('content'); //替換編輯器,editorID爲ckeditor的"id"屬性的值

     $("#content").val(result);    //對editor賦值

     //CKEDITOR.instances.contentCkeditor.setData($("#content").text());

 }

3.接收上傳的圖片並保存在服務端

<?php

ob_start();

//201201/10

$timeDir = date("Ym")."/".date("d");

$uploadDir = dirname(__FILE__).'/upload/'.$timeDir;

$curDomain = "http://".$_SERVER["HTTP_HOST"]."/";

//相對路徑 http://www.ncmem.com/upload/2012-1-10/

$relatPath = $curDomain ."WordPaster2/WordPasterUEditor1x/php/upload/" . $timeDir . "/";

 

//自動建立目錄。upload/2012-1-10

if(!is_dir($uploadDir))

{

    mkdir($uploadDir,0777,true);

}

 

//若是PHP頁面爲UTF-8編碼,請使用urldecode解碼文件名稱

//$fileName = urldecode($_FILES['postedFile']['name']);

//若是PHP頁面爲GB2312編碼,則可直接讀取文件名稱

$fileName = $_FILES['file']['name'];

$tmpName = $_FILES['file']['tmp_name'];

 

//取文件擴展名jpg,gif,bmp,png

$path_parts = pathinfo($fileName);

$ext = $path_parts["extension"];

$ext = strtolower($ext);//jpg,png,gif,bmp

 

//只容許上傳圖片類型的文件

if($ext == "jpg"

    || $ext == "jpeg"

    || $ext == "png"

    || $ext == "gif"

    || $ext == "bmp")

{

    //年_月_日_時分秒毫秒.jpg

    $saveFileName = $fileName;

 

    //xxx/2011_05_05_091250000.jpg

    $savePath = $uploadDir . "/" . $saveFileName;

 

    //另存爲新文件名稱

    if (!move_uploaded_file($tmpName,$savePath))

    {

         exit('upload error!' "文件名稱:" .$fileName . "保存路徑:" . $savePath);

    }

}

 

//輸出圖片路徑

//$_SERVER['HTTP_HOST']   localhost:81

//$_SERVER['REQUEST_URI'] /FCKEditor2.4.6.1/php/test.php

$reqPath = str_replace("upload.php","",$_SERVER['REQUEST_URI']);

echo $relatPath .  $saveFileName;

header('Content-type: text/html; charset=utf-8');

header('Content-Length: ' ob_get_length());

?>


前端效果:

 

接下來就看一下具體操做吧

1、打開工程:

對於文檔的上傳咱們須要知道這個項目的邏輯是否符合咱們的構造。

運行:

 

嘗試使用文檔複製後粘貼進來:

 

圖片上傳進度

 

經過粘貼後,文檔以及圖片被粘貼進來了,看看html代碼是否如咱們的預期:

 

看來這個工程徹底符合咱們的預期,圖片所有使用img標籤統一。傳輸進度條的效果超出了個人意料。

來看看咱們的文檔圖片被放置在哪了:

 

地址:D:\wamp64\www\WordPasterCKEditor4x\php\upload\201904\16

圖片被統一放置在文件夾。

由此看來這個項目的實際效果大大超出了個人意料了,帶入工程後完美的優化了工程項目

工程目錄截圖:

 

 

控件包:

IE(x86)http://t.cn/AiC6segS

IE(x64)http://t.cn/AiCXv7ti

Chromehttp://t.cn/AiC6s86u

Firefoxhttp://t.cn/AiCXvMr5

exehttp://t.cn/AiCXvoVl

 

示例下載:

FCKEditor2xhttp://t.cn/AiCXvOpm

CKEditor3xhttp://t.cn/AiCXvQHu

CKEditor4xhttp://t.cn/AiCXvukn

CuteEditor6xhttp://t.cn/AiCXveOn

KindEditor3xhttp://t.cn/AiCXP7fG

KindEditor4xhttp://t.cn/AiCXPLFu

TinyMCE3xhttp://t.cn/AiCXP5RH

TinyMCE4xhttp://t.cn/AiCXPx1O

UEditor1xhttp://t.cn/AiCXPodH

xhEditor1xhttp://t.cn/AiCXPOhL

HKwik5.0http://t.cn/AiCXPHfA

WordPress3.7.1http://t.cn/AiCXPmrk

Joomla3.4.7http://t.cn/AiCXPgtA

Drupal 7.34http://t.cn/AiCXhzBu