asp.net core linux生成word方案 aspose

代码

using System;
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using AsposeDemo.Models;
using Microsoft.AspNetCore.Hosting;
using Aspose.Words;
using System.IO;

namespace AsposeDemo.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger _logger;
        private IWebHostEnvironment _webHostEnvironment;
        public HomeController(ILogger logger,IWebHostEnvironment webHostEnvironment)
        {
            _logger = logger;
            _webHostEnvironment = webHostEnvironment;
        }

        public IActionResult Index()
        {
        	//注意,在linux的文件路径分隔符与windows下的不同
        	//具体参考:https://docs.microsoft.com/en-us/dotnet/api/system.io.path.directoryseparatorchar?view=netcore-3.1
            var separator = $"{ Path.DirectorySeparatorChar}";
            var path = $"{_webHostEnvironment.ContentRootPath}{separator}Aspose.lic";
            License license = new License();
            license.SetLicense(path);
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
            builder.Writeln("Test中文");
            var result = _webHostEnvironment.WebRootPath + separator + "sample" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".docx";
            doc.Save(result);
            return Content(path + "   "+result);
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

部署方式为docker,不用docker也可以手动复制到linux运行。

你可能感兴趣的:(asp.net core linux生成word方案 aspose)