63 lines
2.0 KiB
C#
63 lines
2.0 KiB
C#
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;
|
||
}
|
||
}
|
||
}
|
||
}
|