需求:使用UEditor上传时需要知道具体到哪个章节得图片,所以得根据Session中得文件重新定义
修改Handler类:
public HttpSessionState Session {get; private set; }
找到上下问得Session:
public Handler(HttpContext context)
{
this.Request = context.Request;
this.Response = context.Response;
this.Context = context;
this.Server = context.Server;
this.Session = context.Session;
}
在controller.ashx中添加
public class UEditorHandler : IHttpHandler,System.Web.SessionState.IRequiresSessionState
实现方法:
public bool IsReusable
{
get
{
return false;
}
}
由于截屏后得图片得名字都一样所以需要随机生成:
在UploadHandler添加:
//var savePath = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat);
//截屏过来的图片文件名固定,固定名称换成随机数
//if (uploadFileName == "image.png")
uploadFileName = random.Next(100000).ToString("D5")+".png"; var zhang = Session[""];
var jie = 2; // Session[""];
var xiaojie = 3; // Session[""];
var savePath = string.Format("upload/image/{0:00}{1:00}{2:00}/{3}",
zhang, jie, xiaojie, uploadFileName);
生成得文件就到了自定义得目录下:
如果想其他UEditor走以前得方法做一个判断:
var savePath = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat);
//截屏过来的图片文件名固定,固定名称换成随机数
//if (uploadFileName == "image.png") //章节内容编辑上传图片,路径名加章节号,文件名称随机
if (Session["zhangClass"] != null)
{
uploadFileName = random.Next(100000).ToString("D5") + ".png";
int zhang = Convert.ToInt32(Session["zhangClass"]);
int jie = Convert.ToInt32(Session["jieClass"]);
int xiaojie = Convert.ToInt32(Session["xiaojieClass"]);
savePath = string.Format("upload/image/{0:00}{1:00}{2:00}/{3}",
zhang, jie, xiaojie, uploadFileName);
}
截屏图片的处理:
private static string GenerateImageFileName(string uploadFileName, string folder)
{ string[] files = Directory.GetFiles(folder);
List<string> fileLenths =new List<string>();
for (int i = 0; i < files.Count(); i++)
{ var fileLenth = files[i].Substring(files[i].LastIndexOf(@"\") + 1); var fileLenthNew = fileLenth.Substring(0, fileLenth.LastIndexOf(".")); fileLenths.Add(fileLenthNew); } var allNames = from file in fileLenths
where file.Length==5 //&& Regex.IsMatch(file, @"^\d{5}\.+$")
orderby file
select file; int num=0;
if (allNames.Count() > 0)
{
var last = allNames.Last();
if (last == null) return 1.ToString("D5");
//num = Convert.ToInt32(last.Substring(0, last.IndexOf(".")));
num = Convert.ToInt32(last);
}
return (num + 1).ToString("D5"); //随机生成
//{
// //uploadFileName = random.Next(100000).ToString("D5")
// // + uploadFileName.Substring(uploadFileName.LastIndexOf("."));//".png";
//} }
判断是否为int
public static bool IsNumeric(string str)
{
if (str == null || str.Length == 0) //验证这个参数是否为空
return false; //是,就返回False
ASCIIEncoding ascii = new ASCIIEncoding();//new ASCIIEncoding 的实例
byte[] bytestr = ascii.GetBytes(str); //把string类型的参数保存到数组里 foreach (byte c in bytestr) //遍历这个数组里的内容
{
if (c < 48 || c > 57) //判断是否为数字
{
return false; //不是,就返回False
}
}
return true; //是,就返回True
}
汉字转拼音:
private static string NormalizeImageFileName(string uploadFileName, string folder)
{ string uploadNum= uploadFileName.Substring(0, uploadFileName.LastIndexOf(".")); if (IsNumeric(uploadNum) == true)
{
return uploadFileName;
}
else {
string pingyin = uploadFileName.Replace(" ", "").Replace("+", "");
var pingyins = PinYinConverterHelp.GetTotalPingYin(uploadFileName);
pingyin = String.Join(",", pingyins.TotalPingYin);
return pingyin;
}
}
拼音扩展类:(nuget安装程序包ChnCharInfo)
public class PinYinConverterHelp
{
public static PingYinModel GetTotalPingYin(string str)
{
var chs = str.ToCharArray();
//记录每个汉字的全拼
Dictionary<int, List<string>> totalPingYins = new Dictionary<int, List<string>>();
for (int i = 0; i < chs.Length; i++)
{
var pinyins = new List<string>();
var ch = chs[i];
//是否是有效的汉字
if (ChineseChar.IsValidChar(ch))
{
ChineseChar cc = new ChineseChar(ch);
pinyins = cc.Pinyins.Where(p => !string.IsNullOrWhiteSpace(p)).ToList();
}
else
{
pinyins.Add(ch.ToString());
} //去除声调,转小写
pinyins = pinyins.ConvertAll(p => Regex.Replace(p, @"\d", "").ToLower());
//去重
pinyins = pinyins.Where(p => !string.IsNullOrWhiteSpace(p)).Distinct().ToList();
if (pinyins.Any())
{
totalPingYins[i] = pinyins;
}
}
PingYinModel result = new PingYinModel();
foreach (var pinyins in totalPingYins)
{
var items = pinyins.Value;
if (result.TotalPingYin.Count <= 0)
{
result.TotalPingYin = items;
result.FirstPingYin = items.ConvertAll(p => p.Substring(0, 1)).Distinct().ToList();
}
else
{
//全拼循环匹配
var newTotalPingYins = new List<string>();
foreach (var totalPingYin in result.TotalPingYin)
{
newTotalPingYins.AddRange(items.Select(item => totalPingYin + item));
}
newTotalPingYins = newTotalPingYins.Distinct().ToList();
result.TotalPingYin = newTotalPingYins; //首字母循环匹配
var newFirstPingYins = new List<string>();
foreach (var firstPingYin in result.FirstPingYin)
{
newFirstPingYins.AddRange(items.Select(item => firstPingYin + item.Substring(0, 1)));
}
newFirstPingYins = newFirstPingYins.Distinct().ToList();
result.FirstPingYin = newFirstPingYins;
}
}
return result;
}
} public class PingYinModel
{
public PingYinModel()
{
TotalPingYin = new List<string>();
FirstPingYin = new List<string>();
} //全拼
public List<string> TotalPingYin { get; set; } //首拼
public List<string> FirstPingYin { get; set; }
}
这样有重复的就可以删除:
try
{
if (!string.IsNullOrEmpty(oldPath)) {
File.Delete(Server.MapPath(oldPath));
}
}
catch (Exception)
{
throw new ArgumentOutOfRangeException("删除原文件,如果不能删除不做处理!!");
//删除原文件,如果不能删除不做处理!!
}