.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);
        }
    }
    
}
相关推荐
我是唐青枫3 小时前
C#.NET ObjectPool 深入解析:对象复用、池化策略与使用边界
c#·.net
aq55356004 小时前
Laravel2.x:被遗忘的PHP框架遗珠
开发语言·汇编·c#
光泽雨4 小时前
c#对object sender ,EventArgs e 的解释
开发语言·c#
Eiceblue4 小时前
C# 删除 PDF 页面:单页 / 多页批量删除技巧
前端·pdf·c#
van久5 小时前
Day15:EF Core 入门 + CodeFirst(就业核心版)
.netcore
1314lay_10076 小时前
Vue+C#根据配置文件实现动态构建查询条件和动态表格
javascript·vue.js·elementui·c#
叫我黎大侠6 小时前
.NET 实战:调用千问视觉模型实现 OCR(车票识别完整教程)
阿里云·ai·c#·ocr·asp.net·.net·.netcore
唐青枫6 小时前
C#.NET ValueTaskSource 深入解析:零分配异步、ManualResetValueTaskSourceCore 与使用边界
c#·.net
公子小六6 小时前
基于.NET的Windows窗体编程之WinForms事件简介
windows·microsoft·c#·.net