CKEditor二次開発---CKEditorに画像をアップロードする機能を追加
7793 ワード
これは私がやった個人知識管理の一部で、今この部分を抽出して、元のコードを加えて、みんなの学習の仕事に対して一定の参考価値があることを望んでいます.
私はJAVAに夢中で、残念ながら、もともとCKFinderで画像のアップロードの機能を完成することができますが、CKFinderはjavaをサポートしていないので、私は自分でCKEditorに手を出すしかありません.実は原理も簡単で、もともとアップロードできるページを私たちが開発したアップロードページに変えるだけです.ここでは、Actionを使って画像のアップロードを実現したり、Actionを必要としない人やActionを知らない人には、その部分の代わりにサーブレットを使ったりする機能がありますが、ここでは、あまりお話ししません.
まず、CKEditorに導入されたjavascriptの部分を修正します.コードは以下の通りです.
これが完了すると、上記のコード「myCkeditor/uploadImage.jsp」のアップロードページを作成できます.//これは私自身の処理ファイル/画像アップロードのページURLです.
次に、上記の
私はJAVAに夢中で、残念ながら、もともとCKFinderで画像のアップロードの機能を完成することができますが、CKFinderはjavaをサポートしていないので、私は自分でCKEditorに手を出すしかありません.実は原理も簡単で、もともとアップロードできるページを私たちが開発したアップロードページに変えるだけです.ここでは、Actionを使って画像のアップロードを実現したり、Actionを必要としない人やActionを知らない人には、その部分の代わりにサーブレットを使ったりする機能がありますが、ここでは、あまりお話ししません.
まず、CKEditorに導入されたjavascriptの部分を修正します.コードは以下の通りです.
<script type="text/javascript">
CKEDITOR.replace('content',addUploadButton(this));
function addUploadButton(editor){
CKEDITOR.on('dialogDefinition', function( ev ){
var dialogName = ev.data.name;
var dialogDefinition = ev.data.definition;
if ( dialogName == 'image' ){
var infoTab = dialogDefinition.getContents( 'info' );
infoTab.add({
type : 'button',
id : 'upload_image',
align : 'center',
label : ' ',
onClick : function( evt ){
var thisDialog = this.getDialog();
var txtUrlObj = thisDialog.getContentElement('info', 'txtUrl');
var txtUrlId = txtUrlObj.getInputElement().$.id;
addUploadImage(txtUrlId);
}
}, 'browse'); //place front of the browser button
}
});
}
function addUploadImage(theURLElementId){
var uploadUrl = "myCkeditor/uploadImage.jsp"; // / URL
var imgUrl = window.showModalDialog(uploadUrl,null,"dialogWidth:360px;dialogHeight:120px;help:no;status:no");
var urlObj = document.getElementById(theURLElementId);
urlObj.value = imgUrl;
//alert(imgUrl);
urlObj.fireEvent("onchange"); // url onchange ,
}
</script>
これが完了すると、上記のコード「myCkeditor/uploadImage.jsp」のアップロードページを作成できます.//これは私自身の処理ファイル/画像アップロードのページURLです.
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title> </title>
</head>
<body>
<iframe name="smz" width="0" height="0" frameborder="0"
style="display: none"></iframe>
<font color="red"><s:property value="#request.errorMessage" /></font>
<form action="imageUpload" method="post" enctype="multipart/form-data"
name="UploadPhoto" target="smz">
<input type="file" name="file" id="file" class="button">
<input type="hidden" name="type" id="type" value=".jpg">
<input onClick="Submit()" type="submit" name="submit" value=" " class="button">
</form>
<input type="hidden" name="pagePath" id="_page_path"
value="<%String p=(String)session.getAttribute("pagePath");if(p!=null)out.print(p);session.removeAttribute("pagePath"); %>" />
<script type="text/javascript">
var _page_path = document.getElementById("_page_path").value;
if(null!=_page_path && ""!=_page_path){
window.returnValue=_page_path;
window.close();
}
function Submit()
{
document.getElementById('type').value=document.getElementById('file').value;
}
</script>
</body>
</html>
次に、上記の