//为编辑器实例添加一个路径,这个不能被注释
UEDITOR_HOME_URL: URL
// 服务器统一请求接口路径,serverUrl: URL + "net/controller.ashx"</code></pre>
controller.ashx
<%@ WebHandler Language="C#" Class="UEditorHandler" %>
using System;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Web;
using System.IO;
using System.Collections;
public class UEditorHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
byte[] uploadFileBytes = null;
string uploadFileName = null;
var json = "";
switch (context.Request["action"])
{
case "config":
json = "{\"imageActionName\":\"uploadimage\",\"imageFieldName\": \"upfile\",\"imageCompressEnable\":\"true\",\"imageCompressBorder\": 1600,\"imageInsertAlign\": \"none\",\"imageUrlPrefix\": \"\",\"imageAllowFiles\": [\".png\",\".jpg\",\".jpeg\",\".gif\",\".bmp\"]}";
break;
case "uploadimage":
var file = context.Request.Files["upfile"];
uploadFileName = file.FileName;
var path = ConfigurationManager.AppSettings["UploadPath"];
var savePath = Format(uploadFileName,path+"{yyyy}{mm}{dd}/{time}{rand:6}");
var localPath = context.Server.MapPath(savePath);
if (!Directory.Exists(Path.GetDirectoryName(localPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(localPath));
}
uploadFileBytes = new byte[file.ContentLength];
file.InputStream.Read(uploadFileBytes,file.ContentLength);
File.WriteAllBytes(localPath,uploadFileBytes);
json = json + "{\"url\":\"" + savePath.Replace("~","") + "\",";
json = json + "\"state\":\"SUCCESS\"}";
//catch (Exception e)
//{
// Result.State = UploadState.FileAccessError;
// Result.ErrorMessage = e.Message;
//}
//finally
//{
// WriteResult();
//}
// break;
//}
//action.Process();
break;
}
WriteJson(context,json);
}
public bool IsReusable
{
get
{
return false;
}
}
protected void WriteJson(HttpContext context,string json)
{
string jsonpCallback = context.Request["callback"];
if (String.IsNullOrWhiteSpace(jsonpCallback))
{
context.Response.AddHeader("Content-Type","text/plain");
context.Response.Write(json);
}
else
{
context.Response.AddHeader("Content-Type","application/javascript");
context.Response.Write(String.Format("{0}({1});",jsonpCallback,json));
}
context.Response.End();
}
private string Format(string originFileName,string pathFormat)
{
if (String.IsNullOrWhiteSpace(pathFormat))
{
pathFormat = "{filename}{rand:6}";
}
var invalidPattern = new Regex(@"[\\\/\:\*\?\042\<\>\|]");
originFileName = invalidPattern.Replace(originFileName,"");
string extension = Path.GetExtension(originFileName);
string filename = Path.GetFileNameWithoutExtension(originFileName);
pathFormat = pathFormat.Replace("{filename}",filename);
pathFormat = new Regex(@"\{rand(\:?)(\d+)\}",RegexOptions.Compiled).Replace(pathFormat,new MatchEvaluator(delegate(Match match)
{
var digit = 6;
if (match.Groups.Count > 2)
{
digit = Convert.ToInt32(match.Groups[2].Value);
}
var rand = new Random();
return rand.Next((int)Math.Pow(10,digit),(int)Math.Pow(10,digit + 1)).ToString();
}));
pathFormat = pathFormat.Replace("{time}",DateTime.Now.Ticks.ToString());
pathFormat = pathFormat.Replace("{yyyy}",DateTime.Now.Year.ToString());
pathFormat = pathFormat.Replace("{yy}",(DateTime.Now.Year % 100).ToString("D2"));
pathFormat = pathFormat.Replace("{mm}",DateTime.Now.Month.ToString("D2"));
pathFormat = pathFormat.Replace("{dd}",DateTime.Now.Day.ToString("D2"));
pathFormat = pathFormat.Replace("{hh}",DateTime.Now.Hour.ToString("D2"));
pathFormat = pathFormat.Replace("{ii}",DateTime.Now.Minute.ToString("D2"));
pathFormat = pathFormat.Replace("{ss}",DateTime.Now.Second.ToString("D2"));
return pathFormat + extension;
}
}