LearnWordManage/Controllers/WordController.cs

63 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<WordController> _logger;
public WordController(ILogger<WordController> logger)
{
_logger = logger;
}
/// <summary>
/// html转换word数据模型
/// </summary>
public class HtmlToPageWordDto
{
[Required]
public string? HtmlBody { get; set; }
public string HeaderName { get; set; } = "";
}
/// <summary>
/// html转换word
/// </summary>
[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;
}
}
}
}