当前位置: 首页 > article >正文

web api 文件上传下载帮助类

web api调用
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;

namespace YiSha.Admin.WebApi.Controllers
{
    [Route("[controller]/[action]")]
    [ApiController]
    [AuthorizeFilter]
    public class FileController : ControllerBase
    {
        #region 上传单个文件
        [HttpPost]
        public async Task<TData<string>> UploadFile(int fileModule, IFormCollection fileList)
        {
            TData<string> obj = await FileHelper.UploadFile(fileModule, fileList.Files);
            return obj;
        }
        #endregion

        #region 删除单个文件
        [HttpPost]
        public TData<string> DeleteFile(int fileModule, string filePath)
        {
            TData<string> obj = FileHelper.DeleteFile(fileModule, filePath);
            return obj;
        }
        #endregion

        #region 下载文件
        [HttpGet]
        public FileContentResult DownloadFile(string filePath, int delete = 1)
        {
            TData<FileContentResult> obj = FileHelper.DownloadFile(filePath, delete);
            if (obj.Tag == 1)
            {
                return obj.Data;
            }
            else
            {
                throw new Exception("下载失败:" + obj.Message);
            }
        }
        #endregion
    }
}

  实现help:

  public class FileHelper
  {
      #region 创建文本文件
      /// <summary>
      /// 创建文件
      /// </summary>
      /// <param name="path"></param>
      /// <param name="content"></param>
      public static void CreateFile(string path, string content)
      {
          if (!Directory.Exists(Path.GetDirectoryName(path)))
          {
              Directory.CreateDirectory(Path.GetDirectoryName(path));
          }
          using (StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8))
          {
              sw.Write(content);
          }
      }
      #endregion

      #region 上传单个文件
      /// <summary>
      /// 上传单个文件
      /// </summary>
      /// <param name="fileModule"></param>
      /// <param name="fileCollection"></param>
      /// <returns></returns>
      public async static Task<TData<string>> UploadFile(int fileModule, IFormFileCollection files)
      {
          string dirModule = string.Empty;
          TData<string> obj = new TData<string>();
          if (files == null || files.Count == 0)
          {
              obj.Message = "请先选择文件!";
              return obj;
          }
          if (files.Count > 1)
          {
              obj.Message = "一次只能上传一个文件!";
              return obj;
          }
          TData objCheck = null;
          IFormFile file = files[0];
          switch (fileModule)
          {
              case (int)UploadFileType.Portrait:
                  objCheck = CheckFileExtension(Path.GetExtension(file.FileName), ".jpg|.jpeg|.gif|.png");
                  if (objCheck.Tag != 1)
                  {
                      obj.Message = objCheck.Message;
                      return obj;
                  }
                  dirModule = UploadFileType.Portrait.ToString();
                  break;

              case (int)UploadFileType.News:
                  if (file.Length > 5 * 1024 * 1024) // 5MB
                  {
                      obj.Message = "文件最大限制为 5MB";
                      return obj;
                  }
                  objCheck = CheckFileExtension(Path.GetExtension(file.FileName), ".jpg|.jpeg|.gif|.png");
                  if (objCheck.Tag != 1)
                  {
                      obj.Message = objCheck.Message;
                      return obj;
                  }
                  dirModule = UploadFileType.News.ToString();
                  break;

              case (int)UploadFileType.Import:
                  objCheck = CheckFileExtension(Path.GetExtension(file.FileName), ".xls|.xlsx");
                  if (objCheck.Tag != 1)
                  {
                      obj.Message = objCheck.Message;
                      return obj;
                  }
                  dirModule = UploadFileType.Import.ToString();
                  break;

              default:
                  obj.Message = "请指定上传到的模块";
                  return obj;
          }
          string fileExtension = TextHelper.GetCustomValue(Path.GetExtension(file.FileName), ".png");

          string newFileName = SecurityHelper.GetGuid(true) + fileExtension;
          string dir = "Resource" + Path.DirectorySeparatorChar + dirModule + Path.DirectorySeparatorChar + DateTime.Now.ToString("yyyy-MM-dd").Replace('-', Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;

          string absoluteDir = Path.Combine(GlobalContext.HostingEnvironment.ContentRootPath, dir);
          string absoluteFileName = string.Empty;
          if (!Directory.Exists(absoluteDir))
          {
              Directory.CreateDirectory(absoluteDir);
          }
          absoluteFileName = absoluteDir + newFileName;
          try
          {
              using (FileStream fs = File.Create(absoluteFileName))
              {
                  await file.CopyToAsync(fs);
                  fs.Flush();
              }
              obj.Data = Path.AltDirectorySeparatorChar + ConvertDirectoryToHttp(dir) + newFileName;
              obj.Message = Path.GetFileNameWithoutExtension(TextHelper.GetCustomValue(file.FileName, newFileName));
              obj.Description = (file.Length / 1024).ToString(); // KB
              obj.Tag = 1;
          }
          catch (Exception ex)
          {
              obj.Message = ex.Message;
          }
          return obj;
      }
      #endregion

      #region 删除单个文件
      /// <summary>
      /// 删除单个文件
      /// </summary>
      /// <param name="fileModule"></param>
      /// <param name="filePath"></param>
      /// <returns></returns>
      public static TData<string> DeleteFile(int fileModule, string filePath)
      {
          TData<string> obj = new TData<string>();
          string dirModule = fileModule.GetDescriptionByEnum<UploadFileType>();

          if (string.IsNullOrEmpty(filePath))
          {
              obj.Message = "请先选择文件!";
              return obj;
          }

          filePath = FilterFilePath(filePath);
          filePath = "Resource" + Path.DirectorySeparatorChar + dirModule + Path.DirectorySeparatorChar + filePath;
          string absoluteDir = Path.Combine(GlobalContext.HostingEnvironment.ContentRootPath, filePath);
          try
          {
              if (File.Exists(absoluteDir))
              {
                  File.Delete(absoluteDir);
              }
              else
              {
                  obj.Message = "文件不存在";
              }
              obj.Tag = 1;
          }
          catch (Exception ex)
          {
              obj.Message = ex.Message;
          }
          return obj;
      }
      #endregion

      #region 下载文件
      /// <summary>
      /// 下载文件
      /// </summary>
      /// <param name="filePath"></param>
      /// <param name="delete"></param>
      /// <returns></returns>
      public static TData<FileContentResult> DownloadFile(string filePath, int delete)
      {
          filePath = FilterFilePath(filePath);
          if (!filePath.StartsWith("wwwroot") && !filePath.StartsWith("Resource"))
          {
              throw new Exception("非法访问");
          }
          TData<FileContentResult> obj = new TData<FileContentResult>();
          string absoluteFilePath = GlobalContext.HostingEnvironment.ContentRootPath + Path.DirectorySeparatorChar + filePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
          byte[] fileBytes = File.ReadAllBytes(absoluteFilePath);
          if (delete == 1)
          {
              File.Delete(absoluteFilePath);
          }
          // md5 值
          string fileNamePrefix = DateTime.Now.ToString("yyyyMMddHHmmss");
          string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
          string title = fileNameWithoutExtension;
          if (fileNameWithoutExtension.Contains("_"))
          {
              title = fileNameWithoutExtension.Split('_')[1].Trim();
          }
          string fileExtensionName = Path.GetExtension(filePath);
          obj.Data = new FileContentResult(fileBytes, "application/octet-stream")
          {
              FileDownloadName = string.Format("{0}_{1}{2}", fileNamePrefix, title, fileExtensionName)
          };
          obj.Tag = 1;
          return obj;
      }
      #endregion 

      #region GetContentType
      public static string GetContentType(string path)
      {
          var types = GetMimeTypes();
          var ext = Path.GetExtension(path).ToLowerInvariant();
          var contentType = types[ext];
          if (string.IsNullOrEmpty(contentType))
          {
              contentType = "application/octet-stream";
          }
          return contentType;
      }
      #endregion

      #region GetMimeTypes
      public static Dictionary<string, string> GetMimeTypes()
      {
          return new Dictionary<string, string>
          {
              {".txt", "text/plain"},
              {".pdf", "application/pdf"},
              {".doc", "application/vnd.ms-word"},
              {".docx", "application/vnd.ms-word"},
              {".xls", "application/vnd.ms-excel"},
              {".xlsx", "application/vnd.openxmlformats officedocument.spreadsheetml.sheet"},
              {".png", "image/png"},
              {".jpg", "image/jpeg"},
              {".jpeg", "image/jpeg"},
              {".gif", "image/gif"},
              {".csv", "text/csv"}
          };
      }
      #endregion

      public static void CreateDirectory(string directory)
      {
          if (!Directory.Exists(directory))
          {
              Directory.CreateDirectory(directory);
          }
      }

      public static void DeleteDirectory(string filePath)
      {
          try
          {
              if (Directory.Exists(filePath)) //如果存在这个文件夹删除之 
              {
                  foreach (string d in Directory.GetFileSystemEntries(filePath))
                  {
                      if (File.Exists(d))
                          File.Delete(d); //直接删除其中的文件                        
                      else
                          DeleteDirectory(d); //递归删除子文件夹 
                  }
                  Directory.Delete(filePath, true); //删除已空文件夹                 
              }
          }
          catch (Exception ex)
          {
              LogHelper.Error(ex);
          }
      }

      public static string ConvertDirectoryToHttp(string directory)
      {
          directory = directory.ParseToString();
          directory = directory.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
          return directory;
      }

      public static string ConvertHttpToDirectory(string http)
      {
          http = http.ParseToString();
          http = http.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
          return http;
      }

      public static TData CheckFileExtension(string fileExtension, string allowExtension)
      {
          TData obj = new TData();
          string[] allowArr = TextHelper.SplitToArray<string>(allowExtension.ToLower(), '|');
          if (allowArr.Where(p => p.Trim() == fileExtension.ParseToString().ToLower()).Any())
          {
              obj.Tag = 1;
          }
          else
          {
              obj.Message = "只有文件扩展名是 " + allowExtension + " 的文件才能上传";
          }
          return obj;
      }

      public static string FilterFilePath(string filePath)
      {
          filePath = filePath.Replace("../", string.Empty);
          filePath = filePath.Replace("..", string.Empty);
          filePath = filePath.TrimStart('/');
          return filePath;
      }
  }


http://www.kler.cn/a/289472.html

相关文章:

  • Qt_day4_Qt_UI设计
  • qt QProcess详解
  • NVIDIA Isaac Sim 仿真平台体验测评
  • 使用pdfjs加载多页pdf并实现打印
  • 在 Ubuntu 上安装 `.deb` 软件包有几种方法
  • 「数据要素」行业简报|2024.11.上刊
  • 生成模型之生成器
  • 使用 Rust 和 Bevy 创建你的第一个三维渲染应用程序
  • 阿尔茨海默病症识别+图像识别Python+人工智能+深度学习+TensorFlow+机器学习+卷积神经网络算法
  • 如何打造一个智能化的远程在线考试系统?
  • 《第二十七章 IO 流 - 字符流》
  • 使用 docker 部署 kvm 图形化管理工具 WebVirtMgr
  • 【鸿蒙HarmonyOS NEXT】List组件的使用
  • 集成电路学习:什么是ARM先进精简指令集计算机
  • Altium Designer爬虫工具/网页信息获取工具
  • 深度学习基础--模型拟合
  • 新版某数字壳脱壳,过frida检测,及重打包
  • uniapp 自定义微信小程序 tabBar 导航栏
  • P1045 麦森数
  • sql92语句与sql99语法的区别
  • MySQL——隔离级别及解决方案
  • 停止和删除所有 Docker 容器的详细指南
  • Unity面向对象补全计划 之 List<T>与class(非基础)
  • CMake构建学习笔记12-libzip库的构建
  • ArkUI-状态管理-@Provide、@Consume、@Observed、@ObjectLink
  • 9\1 numpy基础(二)