Chrome+IE默認支持粘貼剪切板中的圖片,但是我要發布的文章存在word里面,圖片多達數十張,我總不能一張一張復制吧?
Chrome高版本提供了可以將單張圖片轉換在BASE64字符串的功能。但是無法處理多張圖片。而且轉換成BASE64后是作為內容一起提交給服務器,不能夠將圖片單獨保存在另外一台服務器中。如果需要單獨保存則需要自已進行處理。比較麻煩。
我希望打開Word或者WPS文檔后,復制內容然后直接粘貼到富文本編輯器中,編輯器自動將圖片批量上傳到服務器中,無論文檔中有多少張圖片,編輯器都全部自動上傳,不需要再手動一張張處理。同時能夠將圖片上傳到我指定的接口中,服務器需要將圖片單獨保存在存儲服務器中,比如可能是雲存儲,或者是分布式存儲,最后直接發布內容。
感覺這個似乎很困難,因為Ueditor本身不支持,粘貼后直接就是空白,這里面一定有原因。
好,開始嘗試UMeditor,Chrome只能獲得本地路徑,無法讀取文件。
https://ueditor.baidu.com/website/umeditor.html(有興趣可以試試)
難道就這么失敗了?
不,但是我意外發現UMeditor竟然支持粘貼word中的多張圖片(僅支持IE11,不支持IE10以下版本、以及Chrome等)
切換HTML,會看到你的圖片被組織成base64
nice,機會來了,既然IE支持復制word中的多張圖片直接粘貼base64,既然有了base64我們就有辦法上傳轉圖片啦!
那么我們來改造Ueditor,讓他支持IE11(總比沒得用強吧)
打開你的ueditor.all.js(1.4.3版本以下行號根據自己使用的版本可能不同)
1、注釋掉14679行(暫時不明確有什么不良影響)
//執行默認的處理
//me.filterInputRule(root);
2、在28725行插入以下代碼(如果是使用IE11粘貼會得到base64,先用占位符占位,再逐個把base64專成Blob文件並上傳,上傳完成再替換為你的img屬性src為服務器圖片url)
this.WordParser_PasteWord = function (json)
{
this.postType = WordPasteImgType.word;
this.EditorContent = json.word;
for (var i = 0, l = json.imgs.length; i < l; ++i)
{
this.addImgLoc(json.imgs[i]);
}
this.OpenDialogFile();
};
this.WordParser_PasteExcel = function (json)
{
this.postType = WordPasteImgType.word;
this.EditorContent = json.word;
for (var i = 0, l = json.imgs.length; i < l; ++i)
{
this.addImgLoc(json.imgs[i]);
}
this.OpenDialogFile();
};
this.WordParser_PasteHtml = function (json)
{
this.postType = WordPasteImgType.word;
this.InsertHtml(json.word);//
this.working = false;
};
this.WordParser_PasteFiles = function (json)
{
this.postType = WordPasteImgType.local;
for (var i = 0, l = json.imgs.length; i < l; ++i)
{
var task = this.addImgLoc(json.imgs[i]);
task.PostLocalFile = true;//
}
this.OpenDialogFile();
};
this.WordParser_PasteImage = function (json)
{
this.OpenDialogPaste();
this.imgMsg.text("開始上傳");
this.imgPercent.text("1%");
};
this.WordParser_PasteAuto = function (json)
{
this.postType = WordPasteImgType.network;
for (var i = 0, l = json.imgs.length; i < l; ++i)
{
this.addImgLoc(json.imgs[i]);
}
this.OpenDialogFile();
};
this.WordParser_PostComplete = function (json)
{
this.imgPercent.text("100%");
this.imgMsg.text("上傳完成");
var img = "<img src=\"";
img += json.value;
img += "\" />";
this.InsertHtml(img);
this.CloseDialogPaste();
this.working = false;
};
this.WordParser_PostProcess = function (json)
{
this.imgPercent.text(json.percent);
};
this.WordParser_PostError = function (json)
{
this.OpenDialogPaste();
this.imgMsg.text(WordPasterError[json.value]);
this.imgIco.src = this.Config["IcoError"];
this.imgPercent.text("");
};
this.File_PostComplete = function (json)
{
var up = this.fileMap[json.id];
up.postComplete(json);
delete up;//
};
this.File_PostProcess = function (json)
{
var up = this.fileMap[json.id];
up.postProcess(json);
};
this.File_PostError = function (json)
{
var up = this.fileMap[json.id];
up.postError(json);
};
this.Queue_Complete = function (json)
{
//上傳網絡圖片
if (_this.postType == WordPasteImgType.network)
{
_this.GetEditor().setData(json.word);
} //上傳Word圖片時才替換內容
elseif (_this.postType == WordPasteImgType.word)
{
_this.InsertHtml(json.word);//
}
this.CloseDialogFile();
_this.working = false;
};
this.load_complete_edge = function (json)
{
_this.app.init();
};
this.state_change = function (json) {
if (json.value == "parse_document")
{
this.OpenDialogFile();
this.filesPanel.text("正在解析文檔");
}
elseif (json.value == "process_data") {
this.filesPanel.text("正在處理數據");
}
elseif (json.value == "process_data_end")
{
this.filesPanel.text("");
}
};
this.load_complete = function (json)
{
var needUpdate = true;
if (typeof (json.version) != "undefined")
{
this.setuped = true;
if (json.version == this.Config.Version) {
needUpdate = false;
}
}
if (needUpdate) this.need_update();
//else { $.skygqbox.hide(); }
};
this.recvMessage = function (msg)
{
var json = JSON.parse(msg);
if (json.name == "Parser_PasteWord") _this.WordParser_PasteWord(json);
elseif (json.name == "Parser_PasteExcel") _this.WordParser_PasteExcel(json);
elseif (json.name == "Parser_PasteHtml") _this.WordParser_PasteHtml(json);
elseif (json.name == "Parser_PasteFiles") _this.WordParser_PasteFiles(json);
elseif (json.name == "Parser_PasteImage") _this.WordParser_PasteImage(json);
elseif (json.name == "Parser_PasteAuto") _this.WordParser_PasteAuto(json);
elseif (json.name == "Parser_PostComplete") _this.WordParser_PostComplete(json);
elseif (json.name == "Parser_PostProcess") _this.WordParser_PostProcess(json);
elseif (json.name == "Parser_PostError") _this.WordParser_PostError(json);
elseif (json.name == "File_PostProcess") _this.File_PostProcess(json);
elseif (json.name == "File_PostComplete") _this.File_PostComplete(json);
elseif (json.name == "File_PostError") _this.File_PostError(json);
elseif (json.name == "load_complete") _this.load_complete(json);
elseif (json.name == "Queue_Complete") _this.Queue_Complete(json);
elseif (json.name == "load_complete_edge") _this.load_complete_edge(json);
elseif (json.name == "state_change") _this.state_change(json);
};
服務端上傳代碼
using System;
using System.Web;
using System.IO;
namespace WordPasterCK4
{
publicpartialclassupload : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
string fname = Request.Form["UserName"];
int len = Request.ContentLength;
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 nameOri = file.FileName;
string ext = Path.GetExtension(nameOri).ToLower();
string filePathSvr = Path.Combine(folder, nameOri);
Response.Write(uploadPath + nameOri);
}
}
}
}
處理后的效果,能夠批量上傳word中所有的圖片,真的是太方便了。無論多少張圖片都可以一次性批量上傳。這個功能真的是太方便了,大幅度提升了內容編輯人員的效率。以前一天只能發布一篇文章,現在能夠發布100篇,這效率簡直提升了100倍呀。
圖片上傳后保存在服務器端。而且也可以指定上傳接口地址,這個也比較方便。因為我們的業務是將圖片保存在單獨的雲存儲服務器中。
3、處理ueditor提供的uploadimage方法
客戶已經使用半年,沒有問題,非常有用,非常方便的功能
有需要的朋友可以下載:http://blog.ncmem.com/wordpress/2019/08/07/ueditor復制word圖片粘貼上傳-2/