.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);
        }
    }
    
}
相关推荐
阿翰1 小时前
自动 GitHub Readme 20 种语言翻译平台 - OpenAiTx 开源免费
c#·.net
枫叶kx5 小时前
1Panel运行的.net程序无法读取系统字体(因为使用了docker)
c#
军训猫猫头10 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#
不爱写代码的玉子12 小时前
HALCON透视矩阵
人工智能·深度学习·线性代数·算法·计算机视觉·矩阵·c#
开开心心就好14 小时前
高效Excel合并拆分软件
开发语言·javascript·c#·ocr·排序算法·excel·最小二乘法
一名用户16 小时前
unity实现自定义粒子系统
c#·unity3d·游戏开发
钢铁男儿18 小时前
C# 类和继承(扩展方法)
java·servlet·c#
爱炸薯条的小朋友18 小时前
C#由于获取WPF窗口名称造成的异常报错问题
windows·c#·wpf
Rose 使者20 小时前
全球IP归属地查询接口如何用C#进行调用?
c#·api·ip地址
~plus~1 天前
Harmony核心:动态方法修补与.NET游戏Mod开发
开发语言·jvm·经验分享·后端·程序人生·c#