小程序页面之间进行传值的操作办法
790
2022-11-10
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
判断是否为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
这样有重复的就可以删除:
try { if (!string.IsNullOrEmpty(oldPath)) { File.Delete(Server.MapPath(oldPath)); } } catch (Exception) { throw new ArgumentOutOfRangeException("删除原文件,如果不能删除不做处理!!"); //删除原文件,如果不能删除不做处理!! }
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~