using EntrustSettle.Common; using EntrustSettle.Controllers; using EntrustSettle.IServices; using EntrustSettle.Model; using EntrustSettle.Model.Models; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.ComponentModel.DataAnnotations; using System.Web; namespace EntrustSettle.Api.Controllers { /// /// 附件 /// public class AnnexController : BaseApiController { private readonly IAnnexService annexService; private readonly IHttpContextAccessor httpContextAccessor; public AnnexController(IAnnexService annexService, IHttpContextAccessor httpContextAccessor) { this.annexService = annexService; this.httpContextAccessor = httpContextAccessor; } /// /// 附件上传接口 /// /// 附件文件 /// 文件类型 [HttpPost] public async Task> Upload([FromForm] IFormFile file, [FromForm] FileTypeEnum fileType) { if (file == null) { return Failed("附件不能为空"); } //new FileTypeEnumValidator().ValidateAndThrow(fileType); // 文件目录 var dir = Path.Combine(App.WebHostEnvironment.WebRootPath, "files"); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } // 文件名 var newFileName = Guid.NewGuid().ToString("N") + Path.GetExtension(file.FileName); // 完整路径 var fullPath = Path.Combine(dir, newFileName); // 保存 using (var stream = new FileStream(fullPath, FileMode.Create)) { await file.CopyToAsync(stream); } // 保存文件信息到数据库 var model = new Annex() { Name = file.FileName, Path = Path.Combine(@"files", newFileName), Type = (int)fileType, Key = Guid.NewGuid().ToString("N") }; var annexId = await annexService.Add(model); return Success(annexId); } /// /// 文件下载接口 /// /// 文件记录主键 [HttpGet] public async Task DownloadFile([FromQuery] long annexId) { // 如果当前登陆人非本公司,判断要下载的文件所属订单是否属于登陆人所在公司 //if (!App.User.CompanyName.Contains("东胜伟业") && !App.User.CompanyName.Contains("大简云")) //{ // var compId = await orderAnnexService.AsQueryable() // .InnerJoin((an, o) => an.OrderId == o.Id) // .Where(an => an.AnnexId == annexId) // .Select((an, o) => o.CompanyId) // .FirstAsync(); // if (compId != App.User.CompanyId) // { // throw new Exception("附件所属订单与登陆人不匹配"); // } //} var file = await annexService.QueryById(annexId); if (file == null) { throw new FileNotFoundException("文件记录不存在"); } var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, file.Path); if (!System.IO.File.Exists(filePath)) { throw new FileNotFoundException("文件不存在"); } httpContextAccessor.HttpContext.Response.Headers.TryAdd("Access-Control-Expose-Headers", "Content-Disposition,File-Name"); // 读取filePath文件的内容,并返回给客户端 var fileName = HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8); httpContextAccessor.HttpContext.Response.Headers.TryAdd("File-Name", fileName); return PhysicalFile(filePath, "application/octet-stream", fileName); } /// /// 提供给外部的文件下载接口 /// [HttpGet] [AllowAnonymous] public async Task Download([Required][FromQuery] string key) { var file = await annexService.QueryFirst(x => x.Key == key); if (file == null) { throw new FileNotFoundException("文件记录不存在"); } var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, file.Path); if (!System.IO.File.Exists(filePath)) { throw new FileNotFoundException("文件不存在"); } // 读取filePath文件的内容,并返回给客户端 var p = HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8); var p2 = file.Name; HttpContext.Response.Headers.TryAdd("Access-Control-Expose-Headers", "Content-Disposition"); return PhysicalFile(filePath, "application/octet-stream", p); } } }