1. Viết nhật ký vào tập tin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | public static void WriteLogToFile(string namefile,string noidung = "") { try { string Base_Path = "/Log"; String pathCheck = HttpContext.Current.Server.MapPath(Base_Path); if (!Directory.Exists(pathCheck)) //tạo thư mục nếu chưa có Directory.CreateDirectory(pathCheck); Base_Path = "/Log/" + namefile; String path = HttpContext.Current.Server.MapPath(Base_Path); if (!File.Exists(path)) { File.CreateText(path); } Stream stream = File.Open(path, FileMode.Append, FileAccess.Write, FileShare.Write); using (StreamWriter outputFile = new StreamWriter(stream)) { DateTime now = DateTime.Now; string code ="#REGION"+ now.Ticks.ToString(); outputFile.WriteLine($"=============BEGIN {code} ==========="); outputFile.WriteLine(noidung); outputFile.WriteLine($"=============END {code} ==========="); } stream.Close(); } catch(Exception ex) { string mex = ex.Message; } } |
2. Kiểm tra mở rộng tập tin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | public static string[] ExtensionVideo { get { return new string[] {"mp4","mov","avi","gif", "mpeg","flv","wmv","divx","mkv","rmvb","dvd", "3gp", "webm" }; } } public static string[] ExtensionImg { get { return new string[] { "png", "jpg" ,"jpeg"}; } } public static string[] Extensionfile { get { return new string[] { "xls", "xlsx", "doc", "docx", "pdf" }; } } public static TypeModel KiemTraEx(string Ex_)//Kiểm tra quyền { TypeModel role = new TypeModel(); Ex_ = Ex_.ToLower(); if (!string.IsNullOrEmpty(Ex_)) { for (int i = 0; i < Assets.Constant.ExtensionVideo.Length; i++) { if (Assets.Constant.ExtensionVideo[i] == Ex_.Trim()) { role.ex = "." + Assets.Constant.ExtensionVideo[i]; role.type =0; return role; } } for (int i = 0; i < Assets.Constant.ExtensionImg.Length; i++) { if (Assets.Constant.ExtensionImg[i] == Ex_.Trim()) { role.ex = "." + Assets.Constant.ExtensionImg[i]; role.type = 1; return role; } } for (int i = 0; i < Assets.Constant.Extensionfile.Length; i++) { if (Assets.Constant.Extensionfile[i] == Ex_.Trim()) { role.ex = "." + Assets.Constant.Extensionfile[i]; role.type = 2; return role; } } } return role; } |
3. Chuyển đổi Base64 thành hình ảnh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public static Image Base64ToImage(string base64String) { //string base64String = FrontEnd.Classes.Commons.RemoveSpecialCharacters(base64String1); // Convert Base64 String to byte[] byte[] imageBytes = Convert.FromBase64String(base64String); Bitmap tempBmp; using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length)) { // Convert byte[] to Image ms.Write(imageBytes, 0, imageBytes.Length); using (Image image = Image.FromStream(ms, true)) { //Create another object image for dispose old image handler tempBmp = new Bitmap(image.Width, image.Height); Graphics g = Graphics.FromImage(tempBmp); g.DrawImage(image, 0, 0, image.Width, image.Height); } } return tempBmp; } |
4. Chuyển đổi hình ảnh sang Base64
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public static string ImageToBase64(Image image, ImageFormat format) { string base64String; using (MemoryStream ms = new MemoryStream()) { // Convert Image to byte[] image.Save(ms, format); ms.Position = 0; byte[] imageBytes = ms.ToArray(); // Convert byte[] to Base64 String base64String = Convert.ToBase64String(imageBytes); } return base64String; } |
—– TIẾP TỤC —-