123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- 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<OkObjectResult> UploadFile([FromForm] Guid fileRefId, [FromForm] int fileType)
- {
- var files = Request.Form.Files;
- List<SysAttachment> fileList = new List<SysAttachment>();
- 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<OkObjectResult> UploadPicture([FromForm] Guid fileRefId, [FromForm] int fileType)
- {
- var files = Request.Form.Files;
- List<SysAttachment> fileList = new List<SysAttachment>();
- 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<ResultMessage> DeleteFile(Guid fileId)
- {
- return ResultMessage.Success(FileService.DeleteFileById(fileId));
- }
- [HttpGet]
- public async Task<ResultMessage> 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<ImageBase64> 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; }
- }
- }
- }
|