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 = "../upload.jsp"; //这是我自己的处理文件/图片上传的页面URL
var imgUrl = window.showModalDialog(uploadUrl);
//在upload结束后通过js代码window.returnValue=...可以将图片url返回给imgUrl变量。
//更多window.showModalDialog的使用方法参考
var urlObj = document.getElementById(theURLElementId);
urlObj.value = imgUrl;
urlObj.fireEvent("onchange"); //触发url文本框的onchange事件,以便预览图片
}
upload.jsp
<body>
<form action="${pageContext.request.contextPath}/vmscms/up.action" method="POST" name="pos" enctype="multipart/form-data" target="smz" onsubmit="return Validator.Validate(this,3)">
<iframe name="smz" width="0" height="0" frameborder="0" style="display: none">
</iframe>
<font color="red">*</font>上传图片文件
<input type="file" name="file" require="false" dataType="Filter" msg="文件类型非法"/>
<input type="submit" value="上传"/>
</form>
<s:hidden name="pathvalue" id="_page_path"></s:hidden>
</body>
<script type="text/javascript">
var _page_path = document.getElementById("pathvalue").value;
if(null!=_page_path && ""!=_page_path){
window.returnValue=_page_path;
window.close();
}
</script>
后台 action
public String fileUp() {
// 文件上传
if (file != null) {
fileFileName = fileFileName
.substring(fileFileName.lastIndexOf("."));
// 判断图片的格式
if (!fileFileName.equalsIgnoreCase(".png")
&& !fileFileName.equalsIgnoreCase(".jpg")
&& !fileFileName.equalsIgnoreCase(".gif")
&& !fileFileName.equalsIgnoreCase(".bmp")
&& !fileFileName.equalsIgnoreCase(".jpeg")) {
return ACT_ERROR;
}
// 按照日期每天生成一个文件夹
String path = "/upload/cms/"
+ new SimpleDateFormat("yyyyMMdd").format(new Date());
// 判断文件夹是否存在
File destFile = new File(ServletActionContext.getServletContext()
.getRealPath("")
+ path);
if (!destFile.exists()) {
destFile.mkdir();
}
path = path + "/" + UUID.randomUUID() + fileFileName;
try {
FileUtils.copyFile(file, new File(ServletActionContext
.getServletContext().getRealPath("")
+ path));
} catch (IOException e) {
e.printStackTrace();
}
CmsContentInfo c = (CmsContentInfo) ActionContext.getContext()
.getSession().get("cci");
//如果c不为空修改页面点击的上传 如果为空 添加页面点击的上传
if (c != null) {
pathvalue = "../../" + path.substring(1);
ActionContext.getContext().getSession().remove("cci");
c=null;
} else {
pathvalue = "../" + path.substring(1);
}
}
return ACT_SUCCESS;
}