From 0e6c161d4e37221ea327e5f88d87145b172d6696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Fri, 3 Dec 2021 21:56:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=20UploadController.cs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/UploadController.cs | 86 ------------------- 1 file changed, 86 deletions(-) delete mode 100644 ZR.Admin.WebApi/Controllers/UploadController.cs diff --git a/ZR.Admin.WebApi/Controllers/UploadController.cs b/ZR.Admin.WebApi/Controllers/UploadController.cs deleted file mode 100644 index 7702b11..0000000 --- a/ZR.Admin.WebApi/Controllers/UploadController.cs +++ /dev/null @@ -1,86 +0,0 @@ -using Infrastructure; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Options; -using System; -using System.IO; -using System.Linq; -using ZR.Admin.WebApi.Filters; -using ZR.Service.System.IService; - -namespace ZR.Admin.WebApi.Controllers -{ - [Route("[controller]/[action]")] - public class UploadController : BaseController - { - private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); - private OptionsSetting OptionsSetting; - private IWebHostEnvironment WebHostEnvironment; - private ISysFileService SysFileService; - public UploadController(IOptions optionsSetting, IWebHostEnvironment webHostEnvironment, ISysFileService fileService) - { - OptionsSetting = optionsSetting.Value; - WebHostEnvironment = webHostEnvironment; - SysFileService = fileService; - } - /// - /// 存储文件 - /// - /// - /// - [HttpPost] - //[Verify] - //[ActionPermissionFilter(Permission = "system")] - public IActionResult SaveFile([FromForm(Name = "file")] IFormFile formFile) - { - if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传图片不能为空"); - string fileExt = Path.GetExtension(formFile.FileName); - string fileName = FileUtil.HashFileName(Guid.NewGuid().ToString()).ToLower() + fileExt; - string finalFilePath = Path.Combine(WebHostEnvironment.WebRootPath, FileUtil.GetdirPath("uploads"), fileName); - finalFilePath = finalFilePath.Replace("\\", "/").Replace("//", "/"); - - if (!Directory.Exists(Path.GetDirectoryName(finalFilePath))) - { - Directory.CreateDirectory(Path.GetDirectoryName(finalFilePath)); - } - - using (var stream = new FileStream(finalFilePath, FileMode.Create)) - { - formFile.CopyToAsync(stream); - } - - string accessPath = $"{OptionsSetting.Upload.UploadUrl}/{FileUtil.GetdirPath("uploads").Replace("\\", " /")}{fileName}"; - return ToResponse(ResultCode.SUCCESS, accessPath); - } - - /// - /// 存储文件到阿里云 - /// - /// - /// - [HttpPost] - //[Verify] - //[ActionPermissionFilter(Permission = "system")] - public IActionResult SaveFileAliyun([FromForm(Name = "file")] IFormFile formFile) - { - if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空"); - string fileExt = Path.GetExtension(formFile.FileName); - string[] AllowedFileExtensions = new string[] { ".jpg", ".gif", ".png", ".jpeg", ".webp", ".svga", ".xls" }; - int MaxContentLength = 1024 * 1024 * 4; - - if (!AllowedFileExtensions.Contains(fileExt)) - { - return ToResponse(ResultCode.CUSTOM_ERROR, "上传失败,未经允许上传类型"); - } - - if (formFile.Length > MaxContentLength) - { - return ToResponse(ResultCode.CUSTOM_ERROR, "上传文件过大,不能超过 " + (MaxContentLength / 1024).ToString() + " MB"); - } - (bool, string) result = SysFileService.SaveFile("", formFile); - - return ToResponse(ResultCode.SUCCESS, result.Item2); - } - } -}