UEditor上传自定义文件夹
需求:使用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上传自定义文件夹的更多相关文章
- js上传整个文件夹
文件夹上传:从前端到后端 文件上传是 Web 开发肯定会碰到的问题,而文件夹上传则更加难缠.网上关于文件夹上传的资料多集中在前端,缺少对于后端的关注,然后讲某个后端框架文件上传的文章又不会涉及文件夹. ...
- Git上传空文件夹
git上传的文件夹为空的时候 1,先删除空的文件夹 参考:https://www.cnblogs.com/wang715100018066/p/9694532.html 2,这个只能说是技巧不能说是方 ...
- web上传整个文件夹
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...
- WEB上传一个文件夹
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...
- ASP.NET上传一个文件夹
之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...
- linux下怎么样上传下载文件夹
Linux下目录复制:本机->远程服务器 scp -r /home/shaoxiaohu/test1 zhidao@192.168.0.1:/home/test2 test1为源目录,test2 ...
- plupload上传整个文件夹
大容量文件上传早已不是什么新鲜问题,在.net 2.0时代,HTML5也还没有问世,要实现这样的功能,要么是改web.config,要么是用flash,要么是用一些第三方控件,然而这些解决问题的方法要 ...
- java+上传一个文件夹
在web项目中上传文件夹现在已经成为了一个主流的需求.在OA,或者企业ERP系统中都有类似的需求.上传文件夹并且保留层级结构能够对用户行成很好的引导,用户使用起来也更方便.能够提供更高级的应用支撑. ...
- java+上传整个文件夹的所有文件
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 首先我们需要了解的是上传文件三要素: 1.表单提交方式:post (get方式提交有大小 ...
随机推荐
- 欢迎使用 MWeb-Test
首先介绍一下 MWeb 是什么,MWeb 是专业的 Markdown 写作.记笔记.静态博客生成软件. 然后这里重点说明一下:MWeb 有两个模式,外部模式和文档库模式.外部模式中把本地硬盘或 Dro ...
- BIM 3D 数据交换格式 ----张建平(清华女)
1.collada EXPORTER 2.FBX 3D MAX 3.DAE 4.3D中的OBJ文件格式详解 ( http://www.cnblogs.com/slysky/p/408130 ...
- python 中@ 的用法【转】
这只是我的个人理解: 在Python的函数中偶尔会看到函数定义的上一行有@functionName的修饰,当解释器读到@的这样的修饰符之后,会先解析@后的内容,直接就把@下一行的函数或者类作为@后边的 ...
- Nginx 开启目录下载
平时应用中,我们大都用apache搭建下载页面.毕竟Apache搭建起来非常方便,yum安装,创建目录就可以了. 但有时还是需要用nginx配置下载页面.这里就是一个简单的配置nginx下载页面的过程 ...
- 关于CactiEZ自定义气象图的配置
作者:邓聪聪 主要目录: Weathermap主目录:/var/www/html/plugins/weathermap 图片目录(包含背景图标文件):/var/www/html/plugins/wea ...
- html单选按钮用jQuery中prop()方法设置
模拟单选按钮时用jQuery,prop方法来设置. 赋默认选中值:$("#" + id).find("input:radio[value='" + state ...
- bzoj 1175: The stairways of Saharna
一道杨氏矩阵的题,萌新初入门,还不是很懂,这篇 blog 讲的超级好(就是看图有点麻烦) 据说这玩意儿可以代替堆和平衡树用,支持插入.删除.查询,跑得还挺快的(慢着,复杂度好像是 n^2 ? 而且空间 ...
- MySQL数据库的一些方法使用
substring_index(windSpeed,)/3.6 as windSpeed 可将 .8公里.0m/s 进行拆分 嵌套使用replace方法 replace( replace( repla ...
- PHP超精简文章管理系统 Summer Article
2017年3月8日 21:18:43 星期三 git: https://git.oschina.net/myDcool/article.git 截图:
- python学习第41天
# 索引 # 认识mysql中的key # index key 普通索引,能够加速查询,辅助索引 # unique key 唯一 + 索引,辅助索引 # primary key 唯一 + 非空 + 聚 ...