用于140多种文档格式的ASP.NET Core MVC中的Document Viewer

随着数字文档使用量的增加,尤其是在内容管理系统中,在线文档查看器应用程序变得越来越流行。之所以如此受欢迎,是因为您无需购买或安装专用软件程序即可查看多种文档格式。考虑到文档查看器的重要性,我想写一篇有关如何创建通用文档查看器应用程序的文章。

该文档查看器将是ASP.NET Core MVC应用程序,它将针对.NET Core框架。对于后端的文档渲染,我们将使用GroupDocs.Viewer for .NET API-功能强大的File Viewer API,它支持140多种文档类型,包括PDF,Word,Excel,PowerPoint,Visio,CAD,Outlook和许多其他流行的文档格式。

为什么选择.NET Core?

.NET Core是Microsoft对.NET生态系统的宝贵补充。它使开发跨平台应用程序成为可能,而无需开发人员进行任何额外的工作。这就是为什么我选择.NET Core作为目标框架的原因。

在ASP.NET Core中创建文档查看器的步骤

1.在Visual Studio中创建一个新项目。

2.从项目类型中选择.NET Core,并从模板中选择ASP.NET Core Web应用程序。

3.选择Web应用程序(Model-View-Controller),然后单击确定按钮。

4.从NuGet安装GroupDocs.Viewer。

5.打开Views / Home / Index.cshtml文件并将其内容替换为以下内容:

@ {
    ViewData(“ Title”)=“主页”;
}



 类=“行”>
     类=“ col-md-3”>
         类=“ sidenav”>
             id =“加载程序”>
样式=“向左填充:15px”>档案 @if(ViewBag.lstFiles=空)             {                 @foreach(ViewBag.lstFiles中的字符串文件)                 { href =“ https://dev.to/#” onclick =“ ViewDocument('@ file')”>@文件 }             }
类=“ col-md-9”>

预习

id =“内容”>

6.打开Controllers / HomeController.cs,并用以下代码替换类的内容。

public class HomeController : Controller
{
    private readonly IHostingEnvironment _hostingEnvironment;
    private string projectRootPath;
    private string outputPath;
    private string storagePath;
    List<string> lstFiles; 

    public HomeController(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
        projectRootPath = _hostingEnvironment.ContentRootPath;
        outputPath = Path.Combine(projectRootPath, "wwwroot/Content");
        storagePath = Path.Combine(projectRootPath, "storage");
        lstFiles = new List<string>(); 
    }

    public IActionResult Index()
    {
        var files = Directory.GetFiles(storagePath);
        foreach (var file in files)
        {
            lstFiles.Add(Path.GetFileName(file));
        }
        ViewBag.lstFiles = lstFiles; 
        return View();
    }
    (HttpPost)
    public IActionResult OnPost(string FileName)
    {
        int pageCount = 0;
        string imageFilesFolder = Path.Combine(outputPath, Path.GetFileName(FileName).Replace(".", "_"));
        if (!Directory.Exists(imageFilesFolder))
        {
            Directory.CreateDirectory(imageFilesFolder);
        }
        string imageFilesPath = Path.Combine(imageFilesFolder, "page-{0}.png");
        using (Viewer viewer = new Viewer(Path.Combine(storagePath, FileName)))
        {
      //Get document info
            ViewInfo info = viewer.GetViewInfo(ViewInfoOptions.ForPngView(false));
            pageCount = info.Pages.Count;
      //Set options and render document
            PngViewOptions options = new PngViewOptions(imageFilesPath);
            viewer.View(options);
        } 
        return new JsonResult(pageCount);
    } 

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

7.在wwwroot / css / site.css文件中添加以下样式。

.sidenav {
    width: 300px;
    position: fixed;
    z-index: 1;
    left: 0px;
    background: #eee;
    overflow-x: hidden;
    padding: 8px 0;
}
.sidenav a {
    padding: 6px 8px 6px 16px;
    text-decoration: none;
    font-size: 15px;
    color: #2196F3;
    display: block;
}
.sidenav a:hover {
        color: #064579;
    }
.main {
    margin-left: 140px; /* Same width as the sidebar + left position in px */
    font-size: 15px; /* Increased text to enable scrolling */
    padding: 0px 10px;
}
@media screen and (max-height: 450px) {
    .sidenav {
        padding-top: 15px;
    }
        .sidenav a {
            font-size: 20px;
        }
}
#loader {
    position: fixed;
    left: 0px;
    top: 0px;
    width: 100%;
    height: 100%;
    z-index: 9999;
    background: url('../../images/Loading.gif') 50% 50% no-repeat rgb(249,249,249);
}

8.构建应用程序并在您喜欢的浏览器中运行。

下载

该应用程序的完整源代码可在GitHub存储库上找到。

资讯来源:由0x资讯编译自DEV,原文:https://dev.to/usmanaziz/document-viewer-in-asp-net-core-mvc-for-140-document-formats-1a7e ,版权归作者所有,未经许可,不得转载
你可能还喜欢