You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

619 lines
22 KiB
C#

1 year ago
using Microsoft.AspNetCore.Http;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Admin.Core.Common
{
public class FileHelper : IDisposable
{
private bool _alreadyDispose = false;
#region 构造函数
public FileHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
~FileHelper()
{
Dispose(); ;
}
protected virtual void Dispose(bool isDisposing)
{
if (_alreadyDispose) return;
_alreadyDispose = true;
}
#endregion
#region IDisposable 成员
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
#region 取得文件后缀名
/****************************************
* GetPostfixStr
*
* filename:
*
* string filename = "aaa.aspx";
* string s = EC.FileObj.GetPostfixStr(filename);
*****************************************/
/// <summary>
/// 取后缀名
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>.gif|.html格式</returns>
public static string GetPostfixStr(string filename)
{
int start = filename.LastIndexOf(".");
int length = filename.Length;
string postfix = filename.Substring(start, length - start);
return postfix;
}
#endregion
#region 根据文件大小获取指定前缀的可用文件名
/// <summary>
/// 根据文件大小获取指定前缀的可用文件名
/// </summary>
/// <param name="folderPath">文件夹</param>
/// <param name="prefix">文件前缀</param>
/// <param name="size">文件大小(1m)</param>
/// <param name="ext">文件后缀(.log)</param>
/// <returns>可用文件名</returns>
public static string GetAvailableFileWithPrefixOrderSize(string folderPath, string prefix, int size = 1 * 1024 * 1024, string ext = ".log")
{
var allFiles = new DirectoryInfo(folderPath);
var selectFiles = allFiles.GetFiles().Where(fi => fi.Name.ToLower().Contains(prefix.ToLower()) && fi.Extension.ToLower() == ext.ToLower() && fi.Length < size).OrderByDescending(d => d.Name).ToList();
if (selectFiles.Count > 0)
{
return selectFiles.FirstOrDefault().FullName;
}
return Path.Combine(folderPath, $@"{prefix}_{DateTime.Now.DateToTimeStamp()}.log");
}
public static string GetAvailableFileNameWithPrefixOrderSize(string _contentRoot, string prefix, int size = 1 * 1024 * 1024, string ext = ".log")
{
var folderPath = Path.Combine(_contentRoot, "Log");
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
var allFiles = new DirectoryInfo(folderPath);
var selectFiles = allFiles.GetFiles().Where(fi => fi.Name.ToLower().Contains(prefix.ToLower()) && fi.Extension.ToLower() == ext.ToLower() && fi.Length < size).OrderByDescending(d => d.Name).ToList();
if (selectFiles.Count > 0)
{
return selectFiles.FirstOrDefault().Name.Replace(".log", "");
}
return $@"{prefix}_{DateTime.Now.DateToTimeStamp()}";
}
#endregion
#region 写文件
/****************************************
* WriteFile
* ,
* Path:,Strings:
*
* string Path = Server.MapPath("Default2.aspx");
* string Strings = "这是我写的内容啊";
* EC.FileObj.WriteFile(Path,Strings);
*****************************************/
/// <summary>
/// 写文件
/// </summary>
/// <param name="Path">文件路径</param>
/// <param name="Strings">文件内容</param>
public static void WriteFile(string Path, string Strings)
{
if (!File.Exists(Path))
{
FileStream f = File.Create(Path);
f.Close();
}
StreamWriter f2 = new StreamWriter(Path, false, System.Text.Encoding.GetEncoding("gb2312"));
f2.Write(Strings);
f2.Close();
f2.Dispose();
}
/// <summary>
/// 写文件
/// </summary>
/// <param name="Path">文件路径</param>
/// <param name="Strings">文件内容</param>
/// <param name="encode">编码格式</param>
public static void WriteFile(string Path, string Strings, Encoding encode)
{
if (!File.Exists(Path))
{
FileStream f = File.Create(Path);
f.Close();
}
StreamWriter f2 = new StreamWriter(Path, false, encode);
f2.Write(Strings);
f2.Close();
f2.Dispose();
}
#endregion
#region 读文件
/****************************************
* ReadFile
*
* Path:
*
* string Path = Server.MapPath("Default2.aspx");
* string s = EC.FileObj.ReadFile(Path);
*****************************************/
/// <summary>
/// 读文件
/// </summary>
/// <param name="Path">文件路径</param>
/// <returns></returns>
public static string ReadFile(string Path)
{
string s = "";
if (!File.Exists(Path))
s = "不存在相应的目录";
else
{
StreamReader f2 = new StreamReader(Path, System.Text.Encoding.GetEncoding("gb2312"));
s = f2.ReadToEnd();
f2.Close();
f2.Dispose();
}
return s;
}
/// <summary>
/// 读文件
/// </summary>
/// <param name="Path">文件路径</param>
/// <param name="encode">编码格式</param>
/// <returns></returns>
public static string ReadFile(string Path, Encoding encode)
{
string s = "";
if (!File.Exists(Path))
s = "不存在相应的目录";
else
{
StreamReader f2 = new StreamReader(Path, encode);
s = f2.ReadToEnd();
f2.Close();
f2.Dispose();
}
return s;
}
#endregion
#region 追加文件
/****************************************
* FileAdd
*
* Path:,strings:
*
* string Path = Server.MapPath("Default2.aspx");
* string Strings = "新追加内容";
* EC.FileObj.FileAdd(Path, Strings);
*****************************************/
/// <summary>
/// 追加文件
/// </summary>
/// <param name="Path">文件路径</param>
/// <param name="strings">内容</param>
public static void FileAdd(string Path, string strings)
{
StreamWriter sw = File.AppendText(Path);
sw.Write(strings);
sw.Flush();
sw.Close();
}
#endregion
#region 拷贝文件
/****************************************
* FileCoppy
*
* OrignFile:,NewFile:
*
* string orignFile = Server.MapPath("Default2.aspx");
* string NewFile = Server.MapPath("Default3.aspx");
* EC.FileObj.FileCoppy(OrignFile, NewFile);
*****************************************/
/// <summary>
/// 拷贝文件
/// </summary>
/// <param name="OrignFile">原始文件</param>
/// <param name="NewFile">新文件路径</param>
public static void FileCoppy(string orignFile, string NewFile)
{
File.Copy(orignFile, NewFile, true);
}
#endregion
#region 删除文件
/****************************************
* FileDel
*
* Path:
*
* string Path = Server.MapPath("Default3.aspx");
* EC.FileObj.FileDel(Path);
*****************************************/
/// <summary>
/// 删除文件
/// </summary>
/// <param name="Path">路径</param>
public static void FileDel(string Path)
{
File.Delete(Path);
}
#endregion
#region 移动文件
/****************************************
* FileMove
*
* OrignFile:,NewFile:
*
* string orignFile = Server.MapPath("../说明.txt");
* string NewFile = Server.MapPath("http://www.cnAdmins.com/说明.txt");
* EC.FileObj.FileMove(OrignFile, NewFile);
*****************************************/
/// <summary>
/// 移动文件
/// </summary>
/// <param name="OrignFile">原始路径</param>
/// <param name="NewFile">新路径</param>
public static void FileMove(string orignFile, string NewFile)
{
File.Move(orignFile, NewFile);
}
#endregion
#region 在当前目录下创建目录
/****************************************
* FolderCreate
*
* OrignFolder:,NewFloder:
*
* string orignFolder = Server.MapPath("test/");
* string NewFloder = "new";
* EC.FileObj.FolderCreate(OrignFolder, NewFloder);
*****************************************/
/// <summary>
/// 在当前目录下创建目录
/// </summary>
/// <param name="OrignFolder">当前目录</param>
/// <param name="NewFloder">新目录</param>
public static void FolderCreate(string orignFolder, string NewFloder)
{
Directory.SetCurrentDirectory(orignFolder);
Directory.CreateDirectory(NewFloder);
}
#endregion
#region 递归删除文件夹目录及文件
/****************************************
* DeleteFolder
*
* dir:
*
* string dir = Server.MapPath("test/");
* EC.FileObj.DeleteFolder(dir);
*****************************************/
/// <summary>
/// 递归删除文件夹目录及文件
/// </summary>
/// <param name="dir"></param>
/// <returns></returns>
public static void DeleteFolder(string dir)
{
if (Directory.Exists(dir)) //如果存在这个文件夹删除之
{
foreach (string d in Directory.GetFileSystemEntries(dir))
{
if (File.Exists(d))
File.Delete(d); //直接删除其中的文件
else
DeleteFolder(d); //递归删除子文件夹
}
Directory.Delete(dir); //删除已空文件夹
}
}
#endregion
#region 将指定文件夹下面的所有内容copy到目标文件夹下面 果目标文件夹为只读属性就会报错。
/****************************************
* CopyDir
* copy
* srcPath:,aimPath:
*
* string srcPath = Server.MapPath("test/");
* string aimPath = Server.MapPath("test1/");
* EC.FileObj.CopyDir(srcPath,aimPath);
*****************************************/
/// <summary>
/// 指定文件夹下面的所有内容copy到目标文件夹下面
/// </summary>
/// <param name="srcPath">原始路径</param>
/// <param name="aimPath">目标文件夹</param>
public static void CopyDir(string srcPath, string aimPath)
{
try
{
// 检查目标目录是否以目录分割字符结束如果不是则添加之
if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
aimPath += Path.DirectorySeparatorChar;
// 判断目标目录是否存在如果不存在则新建之
if (!Directory.Exists(aimPath))
Directory.CreateDirectory(aimPath);
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
//如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
//string[] fileList = Directory.GetFiles(srcPath);
string[] fileList = Directory.GetFileSystemEntries(srcPath);
//遍历所有的文件和目录
foreach (string file in fileList)
{
//先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
if (Directory.Exists(file))
CopyDir(file, aimPath + Path.GetFileName(file));
//否则直接Copy文件
else
File.Copy(file, aimPath + Path.GetFileName(file), true);
}
}
catch (Exception ee)
{
throw new Exception(ee.ToString());
}
}
#endregion
/// <summary>
/// 获取AppDomain.CurrentDomain.BaseDirectory绝对路径
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string GetAbsolutePath()
{
return AppDomain.CurrentDomain.BaseDirectory;
}
/// <summary>
/// 获取当前程序绝对路径
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string GetProgramPath()
{
return Directory.GetCurrentDirectory();
}
/// <summary>
/// 判断目标文件夹是否存在 如果不存在则创建
/// </summary>
/// <param name="aimPath"></param>
public static void CreateFolder(string aimPath)
{
if (!Directory.Exists(aimPath)) Directory.CreateDirectory(aimPath);
}
/// <summary>
/// 判断目标文件是否存在 如果不存在则创建
/// </summary>
/// <param name="filePath"></param>
public static void CreateFile(string filePath)
{
if (!File.Exists(filePath))
{
FileStream f = File.Create(filePath);
f.Close();
}
}
/// <summary>
/// 判断文件是否存在
/// </summary>
/// <param name="filepath"></param>
/// <returns></returns>
public static bool IsExists(string filepath)
{
return filepath.IsNotEmptyOrNull() && File.Exists(filepath);
}
/// <summary>
/// 创建上传文件
/// </summary>
/// <param name="fullPath"></param>
/// <param name="file"></param>
public static void CreateFile(string fullPath, IFormFile file)
{
using var fs = new FileStream(fullPath, FileMode.Create);
// 复制文件
file.CopyTo(fs);
// 清空缓冲区数据
fs.Flush();
fs.Dispose();
}
/// <summary>
/// 获取文件类型
/// </summary>
/// <param name="FilenameExtension"></param>
/// <returns></returns>
public static string GetContentType(string FilenameExtension)
{
return FilenameExtension switch
{
".bmp" => "image/bmp",
".gif" => "image/gif",
".jpeg" => "image/jpeg",
".jpg" => "image/jpeg",
".png" => "image/jpeg",
".html" => "text/html",
".txt" => "text/plain",
".vsd" => "application/vnd.visio",
".pptx" => "application/vnd.ms-powerpoint",
".ppt" => "application/vnd.ms-powerpoint",
".docx" => "application/msword",
".doc" => "application/msword",
".xml" => "text/xml",
".mp4" => "video/mp4",
".ogg" => "video/ogg",
".flv" => "video/flv",
".avi" => "video/avi",
".wmv" => "video/wmv",
".rmvb" => "video/rmvb",
".mov" => "video/mov",
_ => "image/jpeg"
};
}
/// <summary>
/// 根据文件类型获取后缀名
/// </summary>
/// <param name="FilenameExtension"></param>
/// <returns></returns>
public static string GetExt(string FilenameExtension)
{
return FilenameExtension switch
{
"image/bmp" => ".bmp",
"image/gif" => ".gif",
"image/jpeg" => ".jpeg",
"text/html" => ".html",
"text/plain" => ".txt",
"application/vnd.visio" => ".vsd",
"application/vnd.ms-powerpoint" => ".pptx",
"application/msword" => ".docx",
"text/xml" => ".xml",
"video/mp4" => ".mp4",
"video/ogg" => ".ogg",
"video/flv" => ".flv",
"video/avi" => ".avi",
"video/wmv" => ".wmv",
"video/rmvb" => ".rmvb",
"video/mov" => ".mov",
_ => ".jpeg",
};
}
/// <summary>
/// 获取图片名
/// </summary>
/// <param name="imageName"></param>
/// <returns></returns>
public static string GetPicKey(string imageName)
{
imageName = imageName.Replace("https://", "");
imageName = imageName.Replace("http://", "");
imageName = imageName.Replace(GetPostfixStr(imageName), "");
return imageName;
}
/// <summary>
/// 保存单文件
/// </summary>
/// <param name="files"></param>
/// <param name="webRootPath"></param>
/// <returns></returns>
public static async Task<MessageModel<string>> FileSaveByBlob(IFormFileCollection files, string webRootPath = "")
{
MessageModel<string> model = new MessageModel<string>();
try
{
//文件夹相对路径
var fileRoot = GetFileRoot(FileActionType.Up);
//文件夹绝对路径
webRootPath = webRootPath.IsNotEmptyOrNull() ? webRootPath : GetProgramPath() + fileRoot;
//获取Form提交的文件
long size = files.Sum(f => f.Length);
string showfilePath = string.Empty;
foreach (var formFile in files)
{
if (formFile.Length > 0)
{
//获取文件后缀名
var fileExt = GetExt(formFile.ContentType);
//随机生成新的文件名
string newFileName = System.Guid.NewGuid().ToString() + fileExt;
#region 文件夹不存在则创建
var filePath = webRootPath;
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
#endregion
#region 文件不存在则新建
filePath = webRootPath + newFileName;
showfilePath = fileRoot + newFileName;
CreateFile(filePath);
#endregion
//把上传的文件复制到指定的文件夹中
using (var stream = new FileStream(filePath, FileMode.Create))
{
await formFile.CopyToAsync(stream);
}
}
}
model.success = true;
model.data = showfilePath;
}
catch
{
model.success = false;
model.data = "文件上传失败!";
}
return model;
}
/// <summary>
/// 获取文件相对路径
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static string GetFileRoot(FileActionType type)
{
//根文件夹
var rootPath = Appsettings.app(new string[] { "AppSettings", "FileRootPath" });
switch (type)
{
//上传类型文件夹
case FileActionType.Up:
return rootPath + SysConst.SYS_FILE_UPLOAD_PATH;
//下载类型文件夹
case FileActionType.Dow:
return rootPath + SysConst.SYS_FILE_EXPORT_PATH;
default:
return "";
}
}
}
}