库未使用 Dotnet 核心和 javascript 为 PDFTron 加载,库问题 simpe_wasm

Libraries not loading for PDFTron using Dotnet core and javascript, issue with library simpe_wasm

我正在处理项目 (.Net Core) 中包含多个 PDF 操作的模块之一的要求,例如从 PDF 转换为 HTML,然后编辑 HTML网页视图编辑器,最终将其转换为 PDF。

我使用 JavaScript 提供的代码来使用 web-based PDF 编辑器 (PDFTRON)。我尝试在提供的官方文档 PDFTron MANUAL INTEGRATION GUIDE 的帮助下进行手动集成。当我 运行 通过节点 js 时,我能够成功使用该库,但是当我在 Dotnet 核心中使用它时,它无法加载 simple_wasm/MemGrow.js.mem 文件,该文件是 memgrow.js,出现错误请看下面的截图。

404错误

为 Web 查看器配置的文件夹

代码如下:

Index.cshtml

<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>
    Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.
    <label for="file_upload">Choose A file</label>
    <input type="file" id="file_upload" name="file_upload" accept=".pdf">
</p>
</div>
<div id='viewer' style='width: 1024px; height: 600px;'> </div>
<script src="~/WebViewer/lib/webviewer.min.js"> </script>
<script> 
const input = document.getElementById('file_upload');
  WebViewer({
    path: 'WebViewer/lib', // path to the PDFTron 'lib' folder on your server
    licenseKey: 'Insert commercial license key here after purchase',    
    initialDoc: 'files/sample.pdf',  // You can also use documents on your server
  }, document.getElementById('viewer'))
    .then(instance => {
        const docViewer = instance.docViewer;
        const annotManager = instance.annotManager;
        // const Annotations = instance.Annotations;
        input.addEventListener('change', () => {
            debugger;
            // Get the file from the input
            const file = input.files[0];
            instance.loadDocument(file, { filename: file.name });
        });

        docViewer.on('documentLoaded', () => {
            // call methods relating to the loaded document
        });
    });

通过node js服务器运行ning时工作正常,截图如下,

寻求帮助以在 .net 核心中提供解决方案。

我能够重现并解决这个问题:

在项目的根目录中,Startup.cs需要修改以添加正确的MIME类型,如

// Microsoft.AspNetCore.StaticFiles needs to be added to use the FileExtensionContentTypeProvider type
// https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.staticfiles.fileextensioncontenttypeprovider?view=aspnetcore-3.1
using Microsoft.AspNetCore.StaticFiles;
// ...

namespace aspnetcoreapp
{
    public class Startup
    {
      // ...
      public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            var provider = new FileExtensionContentTypeProvider();
            provider.Mappings[".res"] = "application/octet-stream";
            provider.Mappings[".mem"] = "application/octet-stream";
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles(new StaticFileOptions(){
                ContentTypeProvider = provider
            });

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
    }
}