using Microsoft.AspNetCore.Mvc; namespace DocumentFile.Service.Controllers { [Route("api/[controller]/[action]")] [ApiController] public class DocumentController : ControllerBase { private readonly IUploadService _uploadService; /// /// 构造 /// /// public DocumentController(IUploadService uploadService) { _uploadService = uploadService; } ///// ///// 上传文件模板(doc、docx) ///// ///// 租户唯一标识code ///// 文件 ///// //[HttpPost] //public async Task UploadDocTemplate([FromForm]string code,IFormFile formFile) //{ // ReturnMsg msg = new ReturnMsg(); // if(formFile.Length>0) // { // msg = await this._uploadService.HandleUploadWordFile(formFile,code,""); // } // else // { // msg.error = "请上传文件"; // } // return new JsonResult(msg); //} /// /// 上传文件(doc、docx) /// 本地指定路径 /// /// [HttpPost] public async Task UploadDocWithPath([FromForm]string code, [FromForm] string path, IFormFile formFile) { ReturnMsg msg = new ReturnMsg(); if (formFile.Length > 0) { msg = await this._uploadService.HandleUploadWordFile(formFile,code,path); } else { msg.error = "请上传文件"; } return new JsonResult(msg); } /// /// 上传文件(doc、docx) /// /// [HttpPost] public async Task UploadDocInstance([FromForm] string code, IFormFile formFile) { ReturnMsg msg = new ReturnMsg(); if (formFile.Length > 0) { msg = await this._uploadService.HandleUploadWordFile(formFile, code, ""); } else { msg.error = "请上传文件"; } return new JsonResult(msg); } /// /// 上传图片(jpg、jpeg、png) /// /// [HttpPost] public async Task UploadPhoto([FromForm] string code, IFormFile formFile) { ReturnMsg msg = new ReturnMsg(); if (formFile.Length > 0) { msg = await this._uploadService.HandleUploadImageFile(formFile, code); } else { msg.error = "请上传文件"; } return new JsonResult(msg); } /// /// 删除文件 /// /// /// [HttpPost] public IActionResult DeleteFile([FromForm] string[] delurl) { ReturnMsg msg = new ReturnMsg(); try { if(delurl!=null && delurl.Length > 0) { var dir = AppDomain.CurrentDomain.BaseDirectory; foreach (var item in delurl) { if (!string.IsNullOrEmpty(item)) { var path = dir + item; if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } } } msg.code = 1; } } catch (Exception ex) { NLogProvider.GetInstance().Error(ex); } return new JsonResult(msg); } /// /// 删除文件 /// /// /// [HttpPost] public IActionResult FileLastWriteTime([FromForm] string delurl) { ReturnMsg msg = new ReturnMsg(); try { if (delurl != null && delurl.Length > 0) { var dir = AppDomain.CurrentDomain.BaseDirectory; if (!string.IsNullOrEmpty(delurl)) { var path = dir + delurl; if (System.IO.File.Exists(path)) { var lastWriteTime = System.IO.File.GetLastWriteTime(path); msg.lastWriteTime = lastWriteTime.ToString(); } } msg.code = 1; } } catch (Exception ex) { NLogProvider.GetInstance().Error(ex); } return new JsonResult(msg); } /// /// 下载文件 /// /// /// [HttpPost] public IActionResult Download([FromForm] string url) { try { if (!string.IsNullOrEmpty(url)) { var _webRootPath = AppDomain.CurrentDomain.BaseDirectory; var path = _webRootPath + url; if (System.IO.File.Exists(path)) { return new FileStreamResult(new FileStream(path, FileMode.Open), "application/octet-stream");// { FileDownloadName = FileName }; } } } catch (Exception ex) { NLogProvider.GetInstance().Error(ex); } return BadRequest(); } } }