Files
shgx_tz_mes_backend_sync/ZR.Admin.WebApi/Controllers/UploadController.cs

87 lines
3.5 KiB
C#
Raw Normal View History

2021-08-23 16:57:25 +08:00
using Infrastructure;
2021-09-26 17:01:52 +08:00
using Microsoft.AspNetCore.Hosting;
2021-08-23 16:57:25 +08:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.IO;
2021-11-29 13:46:55 +08:00
using System.Linq;
2021-09-28 17:42:25 +08:00
using ZR.Admin.WebApi.Filters;
2021-11-29 13:46:55 +08:00
using ZR.Service.System.IService;
2021-08-23 16:57:25 +08:00
namespace ZR.Admin.WebApi.Controllers
{
[Route("[controller]/[action]")]
public class UploadController : BaseController
{
private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private OptionsSetting OptionsSetting;
2021-09-26 17:01:52 +08:00
private IWebHostEnvironment WebHostEnvironment;
2021-11-29 13:46:55 +08:00
private ISysFileService SysFileService;
public UploadController(IOptions<OptionsSetting> optionsSetting, IWebHostEnvironment webHostEnvironment, ISysFileService fileService)
2021-08-23 16:57:25 +08:00
{
OptionsSetting = optionsSetting.Value;
2021-09-26 17:01:52 +08:00
WebHostEnvironment = webHostEnvironment;
2021-11-29 13:46:55 +08:00
SysFileService = fileService;
2021-08-23 16:57:25 +08:00
}
/// <summary>
/// 存储文件
/// </summary>
/// <param name="formFile"></param>
/// <returns></returns>
[HttpPost]
2021-11-29 13:46:55 +08:00
//[Verify]
//[ActionPermissionFilter(Permission = "system")]
2021-08-23 16:57:25 +08:00
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;
2021-09-28 17:42:25 +08:00
string finalFilePath = Path.Combine(WebHostEnvironment.WebRootPath, FileUtil.GetdirPath("uploads"), fileName);
finalFilePath = finalFilePath.Replace("\\", "/").Replace("//", "/");
2021-11-29 13:46:55 +08:00
2021-09-28 17:42:25 +08:00
if (!Directory.Exists(Path.GetDirectoryName(finalFilePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(finalFilePath));
}
2021-08-23 16:57:25 +08:00
using (var stream = new FileStream(finalFilePath, FileMode.Create))
{
formFile.CopyToAsync(stream);
}
2021-09-28 17:42:25 +08:00
string accessPath = $"{OptionsSetting.Upload.UploadUrl}/{FileUtil.GetdirPath("uploads").Replace("\\", " /")}{fileName}";
2021-11-29 13:46:55 +08:00
return ToResponse(ResultCode.SUCCESS, accessPath);
}
/// <summary>
/// 存储文件到阿里云
/// </summary>
/// <param name="formFile"></param>
/// <returns></returns>
[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);
2021-08-23 16:57:25 +08:00
}
}
}