87,902
社区成员
发帖
与我相关
我的任务
分享
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 : 'upload',
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 = "..."; //这是我自己的处理文件/图片上传的页面URL
var imgUrl = window.showModalDialog(uploadUrl);
//在upload结束后通过js代码window.returnValue=...可以将图片url返回给imgUrl变量。
//更多window.showModalDialog的使用方法参考
//http://blog.csdn.net/jrq/archive/2010/01/27/5259946.aspx
var urlObj = document.getElementById(theURLElementId);
urlObj.value = imgUrl;
urlObj.fireEvent("onchange"); //触发url文本框的onchange事件,以便预览图片
}
using System;
using System.Web;
namespace me.common
{
/// <summary>
/// 在要引用的面页继承此类即可
/// </summary>
public partial class CkeditorUpfile : System.Web.UI.Page
{
public string msg = "文件上传";
protected override void OnInit(EventArgs e)
{
#region 权限验证部分
msg = "";
Boolean xiao = true;
String HTP = Convert.ToString(Request.Url);
String superadmin = Convert.ToString(Session["superadmin"]);
//if ((superadmin == null || superadmin == "") && ( HTP.IndexOf("localhost")==-1 && HTP.IndexOf("127.0.0.1")==-1 ))
if (superadmin == null || superadmin == "")
{
//Response.Redirect("../user/login.aspx?err=sessions&backurl=" + HTP + "");
msg = "你没有上传文件的权限";
msg += "<BR><BR><a href=/user/login.aspx?err=sessions&backurl=" + HTP + " target=_blank>请先登录</a>";
}
else
{
xiao = true;
}
//权限验证完成
#endregion
HttpPostedFile localFile = Request.Files["NewFile"];
#region 上传目录
String subDir = Convert.ToString(DateTime.Now.ToString("yyyyMM"));
subDir += "/" + Convert.ToString(DateTime.Now.ToString("dd"));
string BaseDir = "/upfiles/" + subDir;
String updir = (string)Session["updir"];
if ((string.IsNullOrEmpty(updir)) != false)
{
BaseDir = "/" + updir + "/" + subDir;
}
if (!System.IO.Directory.Exists(Server.MapPath(BaseDir)))
{ System.IO.Directory.CreateDirectory(Server.MapPath(BaseDir)); }
#endregion
if (xiao == true && localFile != null)
{
//string sUnsafeFileName = System.IO.Path.GetFileName(localFile.FileName);
//string sFileName = Regex.Replace(sUnsafeFileName, @"[\:\*\?\|\/]", "_", RegexOptions.None);
string sExtension = System.IO.Path.GetExtension(localFile.FileName);
sExtension = sExtension.TrimStart('.');
string sFileName = Convert.ToString(DateTime.Now.ToString("HHmmssffff")) + "." + sExtension;//以时间格式重命名
string sFilePath = BaseDir + "/" + sFileName;
string localFileExtension = System.IO.Path.GetExtension(localFile.FileName);
if (localFile.ContentLength > 102400)
{
msg = "上传失败,文件大于 100KB ";
// ConnectorException.Throw(Errors.FileOver100KB);
//this.SendFileUploadResponse(1, true, "", "", "文件过大,请上传小于 100K 的 JPEG 或 GIF 图片。");
}
else if (localFileExtension != ".jpeg" && localFileExtension != ".jpg" && localFileExtension != ".gif")
{
msg = "类型文件不被接受";
}else
{
localFile.SaveAs(sFilePath);
try
{
if (me.common.fileWater.IsAllowedExtension(sFilePath) == true)
{
me.common.fileWater.FileWater(sFilePath);
msg = "文件上传成功";
}
else
{
System.IO.File.Delete(sFilePath);
msg = "文件上传失败,类型不被接受";
// this.SendFileUploadResponse(1, true, "", "", "文件类型不正确,请上传小于 100KB 的 JPEG 或 GIF 图片。");
}
}
catch (Exception )
{
msg = "文件添加水印或验证类型时出错";
}
}
}
}
}
}
<%@ Page language="c#" Trace="false" Inherits="me.common.CkeditorUpfile" AutoEventWireup="false" ValidateRequest="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<head runat="server">
<title>九百游>文件上传</title>
<link href="/css/2.css" rel="stylesheet" type="text/css">
</head>
<body>
<BR /><BR />
<form id="form1" name="form1" method="post" target="_self" enctype="multipart/form-data">
<table width="500" border="0" align="center">
<tr>
<td width="80" height="40" class="dark">文件位置:</td>
<td width="404" height="40">
<label>
<input name="NewFile" type="file" class="noimgButton" />
</label> </td>
</tr>
<tr>
<td height="40"> </td>
<td height="40">
<label>
<input name="Submit" type="button" class="myButton" onclick="javascript:void(form1.submit())" value="确定上传"/>
</label> <input name="Submit2" type="button" onclick="returnfileurl()" class="Button9729" value="返回上页" /></td>
</tr>
</table>
</form>
<table width="500" border="0" align="center">
<tr>
<td height="59" valign="top" class="dark"><%=msg%></td>
</tr>
</table>
</body>
</html>