using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using System.Web; using Bowin.Common.Files; using Bowin.Common.WebModels; using YLShipBuildLandMap.Entity; using YLShipBuildLandMap.Entity.ViewModel; using YLShipBuildLandMap.Services.Common; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Drawing; namespace YLShipBuildLandMap.Web.Controllers.Common { [Route("api/common/[controller]/[action]")] [Authorize] [ApiController] public class FileController : ControllerBase { private IFileService FileService { get; set; } public FileController(IFileService fileService) { FileService = fileService; } [HttpPost] public async Task UploadFile([FromForm] Guid fileRefId, [FromForm] int fileType) { var files = Request.Form.Files; List fileList = new List(); foreach (var formFile in files) { string filePath = await FileHelper.Upload(formFile); fileList.Add(new SysAttachment { AttachmentId = Guid.NewGuid(), Name = Path.GetFileNameWithoutExtension(formFile.FileName), Suffix = Path.GetExtension(formFile.FileName), Url = filePath, ReferenceId = fileRefId, Type = fileType, CreateTime = DateTime.Now, CreateUserId = LoginUser.Current.UserID }); } await FileService.AddFile(fileList); return Ok(new { success = true, msg = "上传成功", item = string.Join(',', fileList.Select(s => s.AttachmentId)) }); } [HttpPost] public async Task UploadPicture([FromForm] Guid fileRefId, [FromForm] int fileType) { var files = Request.Form.Files; List fileList = new List(); foreach (var formFile in files) { string filePath = await FileHelper.Upload(formFile); fileList.Add(new SysAttachment { AttachmentId = Guid.NewGuid(), Name = Path.GetFileNameWithoutExtension(formFile.FileName), Suffix = Path.GetExtension(formFile.FileName), Url = filePath, ReferenceId = fileRefId, Type = fileType, CreateTime = DateTime.Now, CreateUserId = LoginUser.Current.UserID }); } await FileService.AddFile(fileList); return Ok(new { success = true, msg = "上传成功",item = string.Join(',', fileList.Select(s => s.Url)) }); } [HttpGet] public ResultMessage GetFileList(int pageindex, int pagesize, Guid fileRefId, int? fileType) { return ResultMessage.Success(FileService.GetFileList(pageindex, pagesize, fileRefId, fileType)); } [HttpGet] [AllowAnonymous] public FileStreamResult DownloadFile(Guid fileId) { var file = FileService.GetFileById(fileId).Result; var stream = FileHelper.GetFileStream(file.Url); var filename = HttpUtility.UrlEncode(file.Name + file.Suffix, System.Text.Encoding.GetEncoding("UTF-8")); Response.Headers.Add("Content-Disposition", "attachment;filename=" + filename); return new FileStreamResult(stream, "application/octet-stream"); } [HttpGet] public async Task DeleteFile(Guid fileId) { return ResultMessage.Success(FileService.DeleteFileById(fileId)); } [HttpGet] public async Task DelfileByRefID(Guid noDeleteFileId, Guid fileRefId, int? fileType) { return ResultMessage.Success(FileService.DelfileByRefID(noDeleteFileId, fileRefId, fileType)); } [HttpGet] [AllowAnonymous] public ActionResult Get(string fileName, string fileNewName) { fileNewName = HttpUtility.UrlDecode(fileNewName); var physicalPath = Path.Combine(Configuration.Current.AppSettings.TemplatePhysicalPath, fileName); var pdfStream = System.IO.File.Open(physicalPath, System.IO.FileMode.Open); var data = new byte[pdfStream.Length]; pdfStream.Read(data); pdfStream.Close(); var memoryStream = new MemoryStream(data); System.IO.File.Delete(physicalPath); return File(memoryStream, "application/octet-stream", fileNewName, false); } [HttpGet] [AllowAnonymous] public ResultMessage GetImageBase64(Guid fileId) { var file = FileService.GetFileById(fileId).Result; string base64string = ""; int height = 0; int width = 0; //打开文件 //FileStream fileStream = new FileStream(file.Url, FileMode.Open, FileAccess.Read, FileShare.Read); var fileStream = FileServerManager.GetFileStream(file.Url); // 读取文件的 byte[] byte[] bytes = new byte[fileStream.Length]; fileStream.Read(bytes, 0, bytes.Length); base64string = Convert.ToBase64String(bytes); System.Drawing.Image image = System.Drawing.Image.FromStream(fileStream); height = image.Height; width = image.Width; fileStream.Close(); return ResultMessage.Success(new ImageBase64() { Base64Data = base64string, Height = height, Width = width }); } public class ImageBase64 { public string Base64Data { get; set; } public int Height { get; set; } public int Width { get; set; } } } }