using ExtractResFile.AsposeHook; using Microsoft.AspNetCore.Mvc; using Microsoft.Net.Http.Headers; using System.ComponentModel.DataAnnotations; using System.Text; using System.Web; namespace LearnWordManage.Controllers { [ApiController] [Route("[controller]")] public class WordController : ControllerBase { private readonly ILogger _logger; public WordController(ILogger logger) { _logger = logger; } /// /// html转换word数据模型 /// public class HtmlToPageWordDto { [Required] public string? HtmlBody { get; set; } public string HeaderName { get; set; } = ""; } /// /// html转换word /// [HttpPost, Route("htmltopageword")] public FileContentResult HtmlToPageWord(HtmlToPageWordDto req) { Console.WriteLine($"【html转换word】{DateTime.Now} " +req.HeaderName); try { HookManager.StartHook(); if (req.HtmlBody is null) throw new Exception("无效转换内容"); HttpContext.Response.Headers.AccessControlExposeHeaders = "Content-Disposition"; HttpContext.Response.Headers.Add("Content-Disposition", " attachment;filename=" + HttpUtility.UrlEncode(req.HeaderName, Encoding.UTF8).ToUpper() + ".docx"); var byteResult = AsposeWordManage.HtmlToPageWord(req.HtmlBody, req.HeaderName); var fileName = req.HeaderName + ".docx"; var mimeType = "application/ms-word"; return new FileContentResult(byteResult, mimeType) { FileDownloadName = fileName }; } catch (Exception ex) { Console.WriteLine("【html转换word】出现异常," + ex.Message); Console.WriteLine(ex.StackTrace); throw; } } } }