简体   繁体   English

读取 Blazor Wasm 中的本地/静态文件

[英]Read local/static file in Blazor Wasm

My project is created in Blazor WASM ( I do not want to use Blazor server )我的项目是在Blazor WASM中创建的(我不想使用 Blazor 服务器)

I would like to read XSD files from wwwroot:我想从 wwwroot 读取 XSD 个文件:

在此处输入图像描述

Inside my XsdService.cs - c# class I was trying:在我的 XsdService.cs 中 - c# class 我正在尝试:

string pathToXsd = Path.Combine("plcda","extPL_r2.xsd");
string transformataHTML = System.IO.File.ReadAllText(pathToXsd);

However, I always get errors:但是,我总是收到错误:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Could not find a part of the path "/plcda/extPL_r2.xsd".
System.IO.DirectoryNotFoundException: Could not find a part of the path "/plcda/extPL_r2.xsd".

So is there any chance to include custom/static/local files to Blazor WASM?那么是否有机会将自定义/静态/本地文件包含到 Blazor WASM 中? And read them even if app is offline?即使应用程序处于离线状态也能阅读它们吗?

Create a Http GET call to the files.创建对文件的 Http GET 调用。 Think of Blazor wasm as a SPA application.将 Blazor 视为 SPA 应用程序。 All of the files that are required to run your app are downloaded into a users browser.运行您的应用程序所需的所有文件都已下载到用户浏览器中。 Everything else like images are fetched on request.图像等其他所有内容均应要求获取。 Like an images is requested by the browser.就像浏览器请求图像一样。

@inject HttpClient _client

@code {
    async Task GetXDSFile()
    {
        var byteOfTheFile = await _client.GetByteArrayAsync("plcda/extPL_r2.xsd");
    }
}

This sample just fetches the file as byte array.此示例只是将文件作为字节数组获取。 Other version of the Get maybe more sutaible for you like GetStreamAsync .其他版本的 Get 可能更适合您,例如GetStreamAsync

In .NET Core 6 you can add provider mappings to your client project's program.cs to serve static files.在 .NET Core 6 中,您可以将提供程序映射添加到客户端项目的 program.cs 中,以提供 static 文件。

For example I need to load a 3d model (.obj, .mtl files):例如,我需要加载一个 3d model(.obj、.mtl 文件):

Blazor WASM Client Only Project : Blazor WASM 客户端专用项目

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.StaticFiles;


var provider = new FileExtensionContentTypeProvider();
provider.Mappings.Add(".obj", "application/obj");
provider.Mappings.Add(".mtl", "application/mtl");

builder.Services.Configure<StaticFileOptions>(options =>
{
    options.ContentTypeProvider = provider;
});

await builder.Build().RunAsync();

If you have an asp.net core hosted project you can simply put this in the server project's program.cs file instead and you shouldn't need to add nuget package references.如果你有一个 asp.net 核心托管项目,你可以简单地将它放在服务器项目的 program.cs 文件中,而不需要添加 nuget package 引用。

Blazor WASM Asp.net Core Hosted Project : Blazor WASM Asp.net 核心托管项目

using Microsoft.AspNetCore.StaticFiles;


var provider = new FileExtensionContentTypeProvider();
provider.Mappings.Add(".babylon", "application/javascript");
provider.Mappings.Add(".obj", "application/obj");
provider.Mappings.Add(".mtl", "application/mtl");
builder.Services.Configure<StaticFileOptions>(options =>
{
    options.ContentTypeProvider = provider;
});

var app = builder.Build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM