在项目中用百度编辑器ueditor 引入项目后中是无法上传文件的错误,一直没有处理,后来通过别人的思路把ueditor 中的ueditor.config.js 的serverUrl指向修改为自己写的 Controller 方法 不用百度提供的controller.ashx 直接把controller.ashx中的方法写到Controller 中去,然后把相关配置修改就可以了.如下:
原始
public static class Config
{
private static bool noCache = true;
private static JObject BuildItems()
{
var json = File.ReadAllText(HttpContext.Current.Server.MapPath("config.json"));
return JObject.Parse(json);
}
修改后
public static class Config
{
private static bool noCache = true;
private static JObject BuildItems()
{
var json = File.ReadAllText(HttpContext.Current.Server.MapPath("~/Libs/ueditor/net/config.json"));
return JObject.Parse(json);
}
自己写的 Controller 直接是把controller.ashx 中的代码copy过来而已.
public class FileController : Controller
{
public void Testaction()
{
HttpContext context = System.Web.HttpContext.Current;
Handler action = null;
string baseurl = System.Configuration.ConfigurationManager.AppSettings["UploadPath"];
switch (Request["action"])
{
case "config":
action = new ConfigHandler(context);
break;
case "uploadimage":
action = new UploadHandler(context, new UploadConfig()
{
AllowExtensions = Config.GetStringList("imageAllowFiles"),
PathFormat = Config.GetString("imagePathFormat"),
SizeLimit = Config.GetInt("imageMaxSize"),
UploadFieldName = Config.GetString("imageFieldName")
});
break;
case "uploadscrawl":
action = new UploadHandler(context, new UploadConfig()
{
AllowExtensions = new string[] { ".png" },
PathFormat = Config.GetString("scrawlPathFormat"),
SizeLimit = Config.GetInt("scrawlMaxSize"),
UploadFieldName = Config.GetString("scrawlFieldName"),
Base64 = true,
Base64Filename = "scrawl.png"
});
break;
case "uploadvideo":
action = new UploadHandler(context, new UploadConfig()
{
AllowExtensions = Config.GetStringList("videoAllowFiles"),
PathFormat = Config.GetString("videoPathFormat"),
SizeLimit = Config.GetInt("videoMaxSize"),
UploadFieldName = Config.GetString("videoFieldName")
});
break;
case "uploadfile":
action = new UploadHandler(context, new UploadConfig()
{
AllowExtensions = Config.GetStringList("fileAllowFiles"),
PathFormat = Config.GetString("filePathFormat"),
SizeLimit = Config.GetInt("fileMaxSize"),
UploadFieldName = Config.GetString("fileFieldName")
});
break;
case "pdfupload":
action = new UploadHandler(context, new UploadConfig()
{
AllowExtensions = Config.GetStringList("pdfFileAllowFiles"),
PathFormat = baseurl + Config.GetString("pdfFilePathFormat"),
SizeLimit = Config.GetInt("pdfFileMaxSize"),
UploadFieldName = Config.GetString("pdfFileFieldName")
});
break;
case "listimage":
action = new ListFileManager(context, Config.GetString("imageManagerListPath"), Config.GetStringList("imageManagerAllowFiles"));
break;
case "listfile":
action = new ListFileManager(context, Config.GetString("fileManagerListPath"), Config.GetStringList("fileManagerAllowFiles"));
break;
case "catchimage":
action = new CrawlerHandler(context);
break;
default:
action = new NotSupportedHandler(context);
break;
}
action.Process();
}
// GET: File
public ActionResult Index()
{
return View();
}
}
然后在config.json 要修改一下配置
修改前
"imageActionName": "uploadimage", /* 执行上传图片的action名称 */
"imageFieldName": "upfile", /* 提交的图片表单名称 */
"imageMaxSize": 2048000, /* 上传大小限制,单位B */
"imageAllowFiles": [ ".png", ".jpg", ".jpeg", ".gif", ".bmp" ], /* 上传图片格式显示 */
"imageCompressEnable": true, /* 是否压缩图片,默认是true */
"imageCompressBorder": 1600, /* 图片压缩最长边限制 */
"imageInsertAlign": "none", /* 插入的图片浮动方式 */
"imageUrlPrefix": "", /* 图片访问路径前缀 */
"imagePathFormat": "upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
修改后
"imageActionName": "uploadimage", /* 执行上传图片的action名称 */
"imageFieldName": "upfile", /* 提交的图片表单名称 */
"imageMaxSize": 2048000, /* 上传大小限制,单位B */
"imageAllowFiles": [ ".png", ".jpg", ".jpeg", ".gif", ".bmp" ], /* 上传图片格式显示 */
"imageCompressEnable": true, /* 是否压缩图片,默认是true */
"imageCompressBorder": 1600, /* 图片压缩最长边限制 */
"imageInsertAlign": "none", /* 插入的图片浮动方式 */
"imageUrlPrefix": "/file/", /* 图片访问路径前缀 */
"imagePathFormat": "upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
把相关的配置信息修改后就可用了,根据错误提示信息去修改对应位置即可.