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方式提交有大小 ...
随机推荐
- gradle记录
set-env.bat set "JAVA_HOME=G:\jdk" set "CLASSPATH=%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\ ...
- cartographer 安装
Debian 8 Jessie 一顿操作梦如虎,最后不知道咋装上的. 参考:https://www.jianshu.com/p/9922a51ce38f https://blog.csdn.net/p ...
- 移植busybox构建最小根文件系统
Busybox:瑞士军刀,里面装有很多小命令. STEP 1:构建目录结构 创建根文件系统目录,主要包括以下目录/dev /etc /lib /usr /var /proc /tmp /hom ...
- unigui 在单据中,某输入为必填项的 JS代码
给大家分享下在单据中,某输入为必填项,用红框标示的简单处理方法:UniSession.AddJS(UniEdit1.JSName+ '.el.setStyle({"border" ...
- bug笔记(pc)
1.如果a标签中的href没有设置,那么点击的这个按钮的时候,这个页面会自动刷新!!! Bug: <a href=”” class=”btn”></a>类似这种情况,点击a ...
- 请求Jenkins链接返回403
使用python请求Jenkins链接,返回403 1.使用正确的账号密码(Jenkins -> 系统设置 -> 全局安全设置),该账户拥有访问该Jenkins链接的权限 2.代码中的账号 ...
- [记录]一个有趣的url请求(nodejs)
1 前言 IDE是webstrom,跟项目编程语言,应该没有多大关系. 2 现象 两个看起来是一样的url,但是一个能访问一个不能访问. 然后,复制url到console中发现了差异,分别是:file ...
- 安装v2ray+SwitchyOmega使用谷歌***
系统环境:ubuntu18.04 1.安装v2ray 在root用户下执行命令:bash < (curl -L -s https://install.direct/go.sh) $ cd /e ...
- Metrics介绍和Spring的集成
参考: http://colobu.com/2014/08/08/Metrics-and-Spring-Integration/ https://www.cnblogs.com/yangecnu/p/ ...
- pipeline
执行顺序:pipeline 写 pipeline类class Scrapyproject1Pipeline(object): def process_item(self, item, spider): ...