123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using WMS.Util;
- namespace WMS.BZUtil
- {
- public static class FileHelper
- {
- /// <summary>
- /// 读取根目录下面的文件
- /// </summary>
- /// <param name="filePath">文件路径</param>
- /// <returns></returns>
- public static byte[] ReadRoot(string filePath)
- {
- string rootPath = ConfigHelper.GetValue<string>("baseDir") + "/wwwroot";
- string path = rootPath + filePath;
- return File.ReadAllBytes(path);
- }
- /// <summary>
- /// 读取缓存文件
- /// </summary>
- /// <param name="fileName">文件路径</param>
- /// <returns></returns>
- public static byte[] ReadCache(string fileName)
- {
- string rootPath = ConfigHelper.GetValue<string>("baseDir") + "/wwwroot/cache/";
- string path = rootPath + fileName;
- return File.ReadAllBytes(path);
- }
- /// <summary>
- /// 读取缓存文件
- /// </summary>
- /// <param name="fileName">文件路径</param>
- /// <param name="buffer">文件数据</param>
- /// <returns></returns>
- public static void WriteCache(string fileName, byte[] buffer)
- {
- string rootPath = ConfigHelper.GetValue<string>("baseDir") + "/wwwroot/cache/";
- if (!Directory.Exists(rootPath))
- {
- Directory.CreateDirectory(rootPath);
- }
- string path = rootPath + fileName;
- FileInfo file = new FileInfo(path);
- FileStream fs = file.Create();
- fs.Write(buffer, 0, buffer.Length);
- fs.Close();
- }
- /// <summary>
- /// 读取缓存文件
- /// </summary>
- /// <param name="fileName">文件路径</param>
- /// <returns></returns>
- public static void RemoveCache(string fileName)
- {
- string rootPath = ConfigHelper.GetValue<string>("baseDir") + "/wwwroot/cache/";
- string path = rootPath + fileName;
- File.Delete(path);
- }
- /// <summary>
- /// 读取文件
- /// </summary>
- /// <param name="filePath">文件绝对路径</param>
- /// <returns></returns>
- public static byte[] Read(string filePath)
- {
- return File.ReadAllBytes(filePath);
- }
- /// <summary>
- /// 获取文件的传输类型
- /// </summary>
- /// <param name="fileExt">文件扩展名</param>
- /// <returns></returns>
- public static string getContentType(string fileExt)
- {
- string contentType = "";
- switch (fileExt?.ToLower())
- {
- case "jpg":
- case "jpeg":
- case "gif":
- case "png":
- case "webp":
- contentType = "image/" + fileExt.ToLower();
- break;
- case "bmp":
- contentType = "application/x-bmp";
- break;
- case "pdf":
- contentType = "application/" + fileExt.ToLower();
- break;
- case "txt":
- contentType = "text/plain";
- break;
- case "csv":
- contentType = "";
- break;
- case "html":
- contentType = "text/html";
- break;
- default:
- contentType = "application/octet-stream";
- break;
- }
- return contentType;
- }
- }
- }
|