.net core 8版本读取wwwroot目录下的静态文件方法,通过访问接口可以直接信息访问查看的方式

1、安装Nuget依赖包:Microsoft.AspNetCore.StaticFiles

2、在Program.cs中注册中间件

复制代码
app.UseStaticFiles();

3、将业务接口和实现类在Program.cs中注入到运行时

复制代码
builder.Services.AddScoped<IModelServive,ModelServiveImpl>();

4、新建IModelServive接口,用于获取文件类型

复制代码
using Microsoft.AspNetCore.Mvc;

namespace digitization_model.Service;

public interface IModelServive
{
    String getFileType(String fileName);
}

5、定义接口实现类

复制代码
using System.Net;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;

namespace digitization_model.Service.Impl;

public class ModelServiveImpl : IModelServive
{
    public String getFileType(String fileName)
    {
        String extension = Path.GetExtension(fileName).ToLowerInvariant();
        switch (extension)
        {
            case ".txt": return "text/plain";
            case ".html": return "text/html";
            case ".pdf": return "application/pdf";
            case ".doc": case ".docx": return "application/msword";
            case ".xls": case ".xlsx": return "application/vnd.ms-excel";
            // 添加更多类型...
            default: return "application/octet-stream";
        }
    }
}

6、定义控制器

复制代码
using System.Runtime.InteropServices.JavaScript;
using digitization_model.Service;
using Microsoft.AspNetCore.Mvc;

namespace digitization_model.Controller;

[ApiController]
[Route("[controller]")]
public class ModelController : ControllerBase
{
    private readonly IModelServive modelServive;
    private readonly IWebHostEnvironment _env;
    

    public ModelController(IModelServive modelServive,IWebHostEnvironment _env)
    {
        this.modelServive = modelServive;
        this._env = _env;
    }

    
    
    [HttpGet("getFileUrl/modelFilePath/{modelFilePath}/fileName/{fileName}")]
    public IActionResult getFileUrl(String modelFilePath,String fileName)
    {
        try
        {
            string filePath = Path.Combine(_env.WebRootPath+"/"+modelFilePath, fileName);

            if (!System.IO.File.Exists(filePath))
            {
                return NotFound(); // 如果文件不存在,返回404
            }
            // 获取文件类型并设置Content-Type
            string contentType = modelServive.getFileType(fileName);

            // 读取文件内容并作为附件发送,以便浏览器决定如何处理(预览或下载)
            var fileStream = new FileStream(filePath, FileMode.Open);
            return File(fileStream, contentType, fileName);
        }
        catch (Exception ex)
        {
            // 记录异常或处理错误
            return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
        }
    }
    
}
相关推荐
向宇it7 分钟前
【unity组件介绍】URP Decal Projector贴花投影器,将特定材质(贴花)投影到场景中的其他对象上。
游戏·3d·unity·c#·游戏引擎·材质
斯是 陋室9 小时前
在CentOS7.9服务器上安装.NET 8.0 SDK
运维·服务器·开发语言·c++·c#·云计算·.net
inwith11 小时前
C#语法基础总结(超级全面)(二)
开发语言·c#
ヾChen14 小时前
13届蓝桥杯省赛程序设计试题
物联网·学习·蓝桥杯·c#
我是唐青枫19 小时前
C#.NET 泛型详解
开发语言·c#·.net
Yasin Chen19 小时前
C# StringBuilder源码分析
开发语言·c#
神仙别闹19 小时前
基于 .Net Core+MySQL开发(WinForm)翻译平台
数据库·mysql·.netcore
格林威20 小时前
Baumer工业相机堡盟工业相机如何通过YoloV8模型实现人物识别(C#)
开发语言·人工智能·数码相机·yolo·计算机视觉·c#
Rabbb21 小时前
C# JSON 反序列化时,忽略转换失败的属性 JTokenSafeToExtensions
后端·c#·json
三目条件21 小时前
C#将类属性保存到Ini文件方法(利用拓展方法,反射方式获取到分组名和属性名称属性值)
java·开发语言·c#