asp.net+UEditor粘贴word

最近公司作项目须要实现一个功能,在网页富文本编辑器中实现粘贴Word图文的功能。javascript

咱们在网站中使用的Web编辑器比较多,都是根据用户需求来选择的。目前尚未固定哪个编辑器php

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

在网上查了不少资料,UEditor和其它的Web编辑器(富文本编辑器)在Chrome中能够支持单张图片粘贴。可是咱们的用户须要处理的是Word中的图片和文字,通常状况下Word中的图片可能有十几张。有时候有几十张。特别是用户发一些教程或者使用说明类的文档时图片都是大几十张的。 html

在网上找到说UEditor支持word粘贴,试了一下,只支持一张图片的粘贴。多张图片粘贴还须要用户自已手动选择。也就是说若是用户粘贴的Word中包含20张图片的话,那么用户就须要手动选择20次,这种操做用户是不可能接受的。前端

网上找了好久,大部分都有一些不成熟的问题,皇天不负有心人终于让我找到了一个成熟的项目。java

1、前台页面引用代码jquery

<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="index.aspx.cs"Inherits="CKEditor353.index" %>web

<!DOCTYPEhtmlPUBLIC"-//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>

    <title>WordPaster For CKEditor-3.x</title>

    <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/w.edge.js"charset="utf-8"></script>

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

    <scripttype="text/javascript"src="WordPaster/js/w.file.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>

     <scripttype="text/javascript"src="ckeditor/ckeditor.js"></script>

</head>

<body>

     <textareaid="editor1"name="editor1"><imgsrc="http://mat1.gtimg.com/www/images/qq2012/qqlogo_1x.png"width="134"height="44"/></textarea>

    <scripttype="text/javascript">

        var pasterMgr = new WordPasterManager();

        pasterMgr.Config["PostUrl"] = "http://localhost:2797/asp.net/upload.aspx";//这里填网站的上传路径

        pasterMgr.Config["Cookie"] = 'ASP.NET_SessionId=<%=Session.SessionID%>';

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

 

        CKEDITOR.on('instanceReady'function (evt)

        {

            pasterMgr.SetEditor(evt.editor);

        });

        //自定义快捷键

        CKEDITOR.config.keystrokes = [

              [CKEDITOR.CTRL + 86/*V*/'imagepaster']

        ];

        //加载CKEditor编辑器

        CKEDITOR.replace('editor1');

     </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.接收上传的图片并保存在服务端

using System;

using System.Collections.Generic;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.IO;

using System.Text;

 

namespace WordImages.asp.net

{

     publicpartialclassupload : System.Web.UI.Page

     {

         protectedvoid Page_Load(object sender, EventArgs e)

         {

              string fname = Request.Form["UserName"];

              int len = Request.ContentLength;

 

            System.Diagnostics.Debug.Write(Session["ck"]);

 

              if (Request.Files.Count > 0)

              {

                   DateTime timeNow = DateTime.Now;

                   string uploadPath = "/upload/" + timeNow.ToString("yyyyMM") + "/" + timeNow.ToString("dd") + "/";

 

                   string folder = Server.MapPath(uploadPath);

                   //自动建立目录

                   if (!Directory.Exists(folder))

                   {

                       Directory.CreateDirectory(folder);

                   }

                   HttpPostedFile file = Request.Files.Get(0);

                   string ext = Path.GetExtension(file.FileName).ToLower();

                   //只支持图片上传

                   if (ext == ".jpg"

                    || ext == ".jpeg"

                       || ext == ".png"

                       || ext == ".gif"

                       || ext == ".bmp"

                    || ext == ".webp")

                   {

                       string filePath = Path.Combine(folder, file.FileName);

 

                    //

                    if(!Directory.Exists(filePath)) file.SaveAs(filePath);

                       Response.Write(uploadPath + file.FileName);

                   }

              }

         }

     }

}

 

HTTP协议

请求头数据:

 

请求表单数据:

 

前端效果:

 

接下来就看一下具体操做吧

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/AiCaglBC

CKEditor3xhttp://t.cn/AiCagROE

CKEditor4xhttp://t.cn/AiCagum4

CuteEditor6xhttp://t.cn/AiCasJmE

KindEditor3xhttp://t.cn/AiCasa2h

KindEditor4xhttp://t.cn/AiCasoFp

TinyMCE3xhttp://t.cn/AiCasN26

TinyMCE4xhttp://t.cn/AiCasOiM

UEditor1xhttp://t.cn/AiCasl3t

xhEditor1xhttp://t.cn/AiCasTa1

eWebEditorhttp://t.cn/AiCas8aK