.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);
        }
    }
    
}
相关推荐
lph197210 分钟前
快速分页wpf
c#
试行1 小时前
C#System.Runtime.InteropServices.ExternalException (0x80004005): GDI+ 中发生一般性错误。
c#
每日出拳老爷子5 小时前
[C#] 使用TextBox换行失败的原因与解决方案:换用RichTextBox的实战经验
开发语言·c#
程序猿多布13 小时前
C# 值拷贝、引用拷贝、浅拷贝、深拷贝
c#
阿蒙Amon14 小时前
C#随机数生成全面详解:从基础到高级应用
服务器·网络·c#
开开心心_Every14 小时前
便捷的电脑自动关机辅助工具
开发语言·人工智能·pdf·c#·电脑·音视频·sublime text
我要打打代码17 小时前
C#Winform窗体显示模糊的问题
c#
阿蒙Amon17 小时前
C#正则表达式全面详解:从基础到高级应用
开发语言·正则表达式·c#
水果里面有苹果19 小时前
19-C#静态方法与静态类
java·开发语言·c#
吃着火锅x唱着歌20 小时前
LeetCode 3306.元音辅音字符串计数2
算法·leetcode·c#