| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- using Abp.Logging;
- using SixLabors.ImageSharp;
- using VberZero.Tools.StringModel;
- namespace VberZero.Tools.FileHelpers;
- public static class FileUpload
- {
- private static string GetFileExt(string fileName)
- {
- string fileExt = fileName.Substring(fileName.LastIndexOf(".", StringComparison.Ordinal) + 1, fileName.Length - fileName.LastIndexOf(".", StringComparison.Ordinal) - 1);
- return fileExt.ToLower();
- }
- public static string Base64ToFile(this string base64Str, string fileNamePath, IWebHostEnvironment env = null, bool webRoot = true)
- {
- string lcRetVal = "error@";
- try
- {
- string path = $"{(env == null ? AppDomain.CurrentDomain.BaseDirectory : webRoot ? env.WebRootPath : env.ContentRootPath)}{fileNamePath}";
- SaveBase64File(base64Str, path);
- lcRetVal = fileNamePath;
- }
- catch (Exception e)
- {
- LogHelper.LogException(e);
- lcRetVal += "文件上传异常。";
- }
- return lcRetVal;
- }
- public static string Base64ToPng(this string base64String, string fileNamePath, IWebHostEnvironment env = null, bool webRoot = true)
- {
- string lcRetVal = "error@";
- try
- {
- string path = $"{(env == null ? AppDomain.CurrentDomain.BaseDirectory : webRoot ? env.WebRootPath : env.ContentRootPath)}{fileNamePath}";
- SaveBase64Png(base64String, path);
- lcRetVal = fileNamePath;
- }
- catch (Exception e)
- {
- LogHelper.LogException(e);
- lcRetVal += "文件上传异常。";
- }
- return lcRetVal;
- }
- public static string Base64ToFile(this string base64Str, string fileName, string filePath, string fileExt, IWebHostEnvironment env = null, bool webRoot = true)
- {
- fileName = $"{fileName}.{fileExt}";
- string lcRetVal = Base64ToFile(base64Str, fileName, filePath, env, webRoot);
- return lcRetVal;
- }
- public static string Base64ToFile(this string base64Str, string fileName, string filePath, IWebHostEnvironment env = null, bool webRoot = true)
- {
- string lcRetVal = "error@";
- try
- {
- filePath = filePath.Sw("\\");
- filePath = filePath.Ew("\\");
- string path = $"{(env == null ? AppDomain.CurrentDomain.BaseDirectory : webRoot ? env.WebRootPath : env.ContentRootPath)}{filePath}";
- if (!Directory.Exists(path))
- Directory.CreateDirectory(path);
- SaveBase64File(base64Str, path, fileName);
- lcRetVal = filePath + fileName;
- }
- catch (Exception e)
- {
- LogHelper.LogException(e);
- lcRetVal += "文件上传异常。";
- }
- return lcRetVal;
- }
- public static string Base64ToPng(this string base64String, string fileName, string filePath, IWebHostEnvironment env = null, bool webRoot = true)
- {
- string lcRetVal = "error@";
- try
- {
- fileName = $"{fileName}-{DateTime.Now:yyMMddHHmmss}{new Random().Next(1000, 9999)}.png";
- filePath = filePath.Sw("\\");
- filePath = filePath.Ew("\\");
- string path = $"{(env == null ? AppDomain.CurrentDomain.BaseDirectory : webRoot ? env.WebRootPath : env.ContentRootPath)}{filePath}";
- SaveBase64Png(base64String, path, fileName);
- lcRetVal = filePath + fileName;
- }
- catch (Exception e)
- {
- LogHelper.LogException(e);
- lcRetVal += "文件上传异常。";
- }
- return lcRetVal;
- }
- public static void SaveBase64File(this string base64String, string fileNamePath)
- {
- if (fileNamePath == null)
- {
- return;
- }
- var filePath = fileNamePath.Substring(0, fileNamePath.LastIndexOf('\\'));
- if (!Directory.Exists(filePath))
- Directory.CreateDirectory(filePath);
- byte[] bytes = Convert.FromBase64String(base64String);
- using FileStream fs = new FileStream(fileNamePath, FileMode.Create, FileAccess.Write);
- fs.Write(bytes, 0, bytes.Length);
- fs.Close();
- }
- public static void SaveBase64Png(this string base64String, string fileNamePath)
- {
- if (fileNamePath == null)
- {
- return;
- }
- var filePath = fileNamePath.Substring(0, fileNamePath.LastIndexOf('\\'));
- if (!Directory.Exists(filePath))
- Directory.CreateDirectory(filePath);
- base64String = base64String.StartsWith("data") ? base64String.Split(',')[1] : base64String;
- byte[] bytes = Convert.FromBase64String(base64String);
- //using MemoryStream ms = new MemoryStream(bytes);
- var img = Image.Load(bytes);
- img.SaveAsPng(fileNamePath);
- }
- public static void SaveBase64File(this string base64String, string fileNamePath, string fileName)
- {
- if (fileNamePath == null)
- {
- return;
- }
- if (!Directory.Exists(fileNamePath))
- Directory.CreateDirectory(fileNamePath);
- byte[] bytes = Convert.FromBase64String(base64String);
- using FileStream fs = new FileStream(Path.Combine(fileNamePath, fileName), FileMode.Create, FileAccess.Write);
- fs.Write(bytes, 0, bytes.Length);
- fs.Close();
- }
- public static void SaveBase64Png(this string base64String, string fileNamePath, string fileName)
- {
- if (fileNamePath == null)
- {
- return;
- }
- if (!Directory.Exists(fileNamePath))
- Directory.CreateDirectory(fileNamePath);
- base64String = base64String.StartsWith("data") ? base64String.Split(',')[1] : base64String;
- byte[] bytes = Convert.FromBase64String(base64String);
- //using MemoryStream ms = new MemoryStream(bytes);
- var img = Image.Load(bytes);
- img.SaveAsPng(Path.Combine(fileNamePath, fileName));
- }
- public static void DeleteFile(this string fileNamePath, IWebHostEnvironment env = null, bool webRoot = true)
- {
- try
- {
- fileNamePath = fileNamePath.Sw("\\");
- string path = $"{(env == null ? AppDomain.CurrentDomain.BaseDirectory : webRoot ? env.WebRootPath : env.ContentRootPath)}{fileNamePath}";
- if (File.Exists(path))
- File.Delete(path);
- }
- catch
- {
- //
- }
- }
- }
|