1、首先看下官方网站说明,只需要把黑框的代码复制出来,粘贴到调用UEditor 页面上,当JS加载的时候执行黑框的代码。
案列:当打开页面的的时候就加载 loandUEditor 方法。
function loandUEditor() {
var params = UE.getEditor('e_params'); //e_params为编辑器的id
UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
UE.Editor.prototype.getActionUrl = function (action) {
if (action == 'uploadimage' || action == 'uploadscrawl' || action == 'uploadimage') {
return '@Url.Action("UploadImg", "WebApi")?Type=Product';//远程服务器地址,也可以通过先请求后台然后通过后台在上传到远程服务器上
} else if (action == 'uploadvideo') {
return '@Url.Action("UploadImg", "WebApi")?Type=Product';
} else {
return this._bkGetActionUrl.call(this, action);
}
}
}
我的做法是先提交到本地后台,在后台上传远程服务器上
后台代码:
案列:
#region 上传图片
//通过后台处理,将图片上传到远程服务器上。
public ActionResult UploadImg()
{
HttpPostedFileBase file = Request.Files["upfile"];
string type = DataFormater.GetSafeString(Request["Type"]);
if (file == null)
{
return Content(JsonHelper.GetJson(new { state = "SUCCESS", url = "", title = "", original = "" }));
}
if (file.ContentLength == 0)
return Content(JsonHelper.GetJson(new { state = "请选择标题图片", url = "", title = "", original = "", error = "请选择标题图片" }));
string filepath = string.Format("/UploadFiles/{2}/{0}/{1}", DateTime.Now.Year, DateTime.Now.Month, type);
string error = "";
filepath = base.UploadImage(file, filepath, out error);
if (!string.IsNullOrEmpty(error))
return Content(JsonHelper.GetJson(new { state = error, url = "", title = "", original = "", error = error }));
string[] str = filepath.Split('/');
string filename = str.Last();
return Content(JsonHelper.GetJson(new { state = "SUCCESS", url = filepath, title = filename, original = filename, error = error }));
}
#endregion
后台返回格式必须为:
{
"state":"SUCCESS",
"url":"http://192.168.199.86:8008/APP/UploadFiles/Artice/2017/3/3d3936d8c8f344deafbf25e03e7f7372.png",
"title":"3d3936d8c8f344deafbf25e03e7f7372.png",
"original":"3d3936d8c8f344deafbf25e03e7f7372.png",
"error":""
}
这时候图片能正常上传,但是在编辑器内会出现图片显示错误400,这是编辑器的显示路径错误。需要修改UEditor》net》config.json内
修改配置文件的 将有“图片访问路径前缀”注释的参数修改,如: "scrawlUrlPrefix": "../", /* 图片访问路径前缀 */ ,将参数改成 scrawlUrlPrefix": "" ,只需要把参数的值设置为空就行了。
后台返回给前台json 格式,如state 这类的参数 值可以在UEditor》net》App_Code》Handler.cs 里没找得到。
后台返回的json 格式必须为: