需求:使用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("删除原文件,如果不能删除不做处理!!");
//删除原文件,如果不能删除不做处理!!
}

  

 

UEditor上传自定义文件夹的更多相关文章

  1. js上传整个文件夹

    文件夹上传:从前端到后端 文件上传是 Web 开发肯定会碰到的问题,而文件夹上传则更加难缠.网上关于文件夹上传的资料多集中在前端,缺少对于后端的关注,然后讲某个后端框架文件上传的文章又不会涉及文件夹. ...

  2. Git上传空文件夹

    git上传的文件夹为空的时候 1,先删除空的文件夹 参考:https://www.cnblogs.com/wang715100018066/p/9694532.html 2,这个只能说是技巧不能说是方 ...

  3. web上传整个文件夹

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...

  4. WEB上传一个文件夹

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...

  5. ASP.NET上传一个文件夹

    之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...

  6. linux下怎么样上传下载文件夹

    Linux下目录复制:本机->远程服务器 scp -r /home/shaoxiaohu/test1 zhidao@192.168.0.1:/home/test2 test1为源目录,test2 ...

  7. plupload上传整个文件夹

    大容量文件上传早已不是什么新鲜问题,在.net 2.0时代,HTML5也还没有问世,要实现这样的功能,要么是改web.config,要么是用flash,要么是用一些第三方控件,然而这些解决问题的方法要 ...

  8. java+上传一个文件夹

    在web项目中上传文件夹现在已经成为了一个主流的需求.在OA,或者企业ERP系统中都有类似的需求.上传文件夹并且保留层级结构能够对用户行成很好的引导,用户使用起来也更方便.能够提供更高级的应用支撑. ...

  9. java+上传整个文件夹的所有文件

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 首先我们需要了解的是上传文件三要素: 1.表单提交方式:post (get方式提交有大小 ...

随机推荐

  1. 【转】Python之系统交互(subprocess)

    [转]Python之系统交互(subprocess) 本节内容 os与commands模块 subprocess模块 subprocess.Popen类 总结 我们几乎可以在任何操作系统上通过命令行指 ...

  2. 876. Middle of the Linked List

    1. 原始题目 Given a non-empty, singly linked list with head node head, return a middle node of linked li ...

  3. Redis Cluster高可用集群在线迁移操作记录【转】

    之前介绍了redis cluster的结构及高可用集群部署过程,今天这里简单说下redis集群的迁移.由于之前的redis cluster集群环境部署的服务器性能有限,需要迁移到高配置的服务器上.考虑 ...

  4. for..of和for..in和map、filter等循环区别

    1.for in遍历的是数组的索引(即键名),而for of遍历的是数组元素值. for in遍历比较适合遍历对象,不太适合数组,有可能遍历出来的不按照顺序 遍历数组 ,,,,,] for (var ...

  5. CFtpConnection Class

    CFtpConnection Class   1.链接http://technet.microsoft.com/zh-cn/office/2kywsafk(v=vs.80) 2.测试ftp可以用这个地 ...

  6. CF449C:Jzzhu and Apples

    题意简述 给出正整数n,你要把1-n之间的正整数分成尽可能多组,使得每一组两个数的最大公约数大于1;输出能分成最多组的个数,并按任意顺序输出每组的两个数. 很妙的一道题. 首先我们考虑去处理每个质数的 ...

  7. 023_nginx跨域问题

    什么是跨域? 使用js获取数据时,涉及到的两个url只要协议.域名.端口有任何一个不同,都被当作是不同的域,相互访问就会有跨域问题.例如客户端的域名是www.redis.com.cn,而请求的域名是w ...

  8. Fiddler功能介绍

    1.对话框:添加备注,添加完了会在控制面板中的comments显示2.Replay:选中会话后点击,会重新发送请求3.Go:是打断点后,想要继续执行,就点击GO 4.Stream:模式切换. 默认是缓 ...

  9. Find Lines

    (Uva 6955可以直接随机,湖大OJ 13348 要优化) 题意:给出 n个点的坐标, 一个 百分数p, 求是否有一条直线上有 n * p /100个点… 随机化算法,但也要优化下……(TLE, ...

  10. MS SQL Server 增删改查

    数据插入 语法:INSERT INTO Table_name(field1,field2……fieldN) values(value1,vlaue2,…valueN) 单行插入用户类型 INSERT ...