MVC使用Ueditor首先进行配置,
前台配置
ue = UE.getEditor('container', {
toolbars: [
[
'source',
'bold',
'italic',
'underline',
'fontborder',
'strikethrough',
//'superscript',
//'subscript',
'removeformat',
'formatmatch',
'autotypeset',
'blockquote',
'pasteplain',
'|',
'fontsize', //字号
'justifyleft',
'justifycenter',
'justifyright',
'justifyjustify',
'forecolor',
'backcolor',
'insertorderedlist',
'insertunorderedlist',
'selectall',
'lineheight', //行间距
'simpleupload', //单图上传
'insertimage', //多图上传
'cleardoc']
],
//关闭字数统计
wordCount: false,
//关闭elementPath
elementPathEnabled: false,
initialFrameWidth: '100%',
initialFrameHeight: 345,
scaleEnabled: false,
autoHeightEnabled: false,
serverUrl: '/Utility/UeditorUpload'
});
在后台使用 url 获取 action
if (sAction.Equals("uploadimage") )
{
//上传图片
//上传文件
HttpPostedFileBase file = Request.Files["FileData"];
string dstPath = ""; //目标目录
string fileName = ""; //文件名(不带扩展名)
string ext = ""; //扩展名.jgp
string newFileName = "";//新文件名 150404054.jgp
//上传图片
if (file != null)
{
fileName = Path.GetFileNameWithoutExtension(file.FileName);
ext = Path.GetExtension(file.FileName); //.jpg
newFileName = DateTime.Now.ToString("yyMMddHHmmssfff") + ext;
dstPath = "/" + DateTime.Now.ToString("yyyyMMdd") + "/" + ext.Substring(1);
if (!Directory.Exists(localPath + dstPath))
{
//创建目录
Directory.CreateDirectory(localPath + dstPath);
}
file.SaveAs(localPath + dstPath + "/" + newFileName);
sUrl = string.Format("/defaultFiles" + dstPath + "/" + newFileName);
sTitle = newFileName;
}
return Json(new { state = sState, url = sUrl, title = sTitle, original = sOriginal, error = sError }, "application/json", System.Text.Encoding.UTF8, JsonRequestBehavior.AllowGet);
}
else if (sAction.Equals("config"))
{
string [] allowFiles = new string[] {".png", ".jpg", ".jpeg", ".gif", ".bmp"};
//获取配置
return Json(
new {
imageActionName = "uploadimage", /* 执行上传图片的action名称 */ //后台的 action 值
imageFieldName = "FileData", /* 提交的图片表单名称 */
imageMaxSize= 2048000, /* 上传大小限制,单位B */
imageAllowFiles= allowFiles,// {".png", ".jpg", ".jpeg", ".gif", ".bmp"}, /* 上传图片格式显示 */
imageCompressEnable= true, /* 是否压缩图片,默认是true */
imageCompressBorder= 1600, /* 图片压缩最长边限制 */
imageInsertAlign= "none", /* 插入的图片浮动方式 */
imageUrlPrefix= "" , /* 图片访问路径前缀 */
imagePathFormat= "", /* 上传保存路径,可以自定义保存路径和文件名格式 */
/* {filename} 会替换成原文件名,配置这项需要注意中文乱码问题 */
/* {rand:6} 会替换成随机数,后面的数字是随机数的位数 */
/* {time} 会替换成时间戳 */
/* {yyyy} 会替换成四位年份 */
/* {yy} 会替换成两位年份 */
/* {mm} 会替换成两位月份 */
/* {dd} 会替换成两位日期 */
/* {hh} 会替换成两位小时 */
/* {ii} 会替换成两位分钟 */
/* {ss} 会替换成两位秒 */
/* 非法字符 \ : * ? " < > | */
/* 具请体看线上文档: fex.baidu.com/ueditor/#use-format_upload_filename */
},
"application/json",
System.Text.Encoding.UTF8,
JsonRequestBehavior.AllowGet);
}
else
{
sState = "无法识别";
sError = "";
return Json(new { state = sState, url = sUrl, title = sTitle, original = sOriginal, error = sError }, "application/json", System.Text.Encoding.UTF8, JsonRequestBehavior.AllowGet);
}