.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);
        }
    }
    
}
相关推荐
almighty275 小时前
C#WPF控制USB摄像头参数:曝光、白平衡等高级设置完全指南
开发语言·c#·wpf·usb相机·参数设置
后青春期的诗go5 小时前
金蝶云星空插件开发记录(一)
c#·钉钉·金蝶云星空·插件开发
大飞pkz11 小时前
【设计模式】题目小练1
开发语言·设计模式·c#·题目小练
lljss202011 小时前
C# 每个chartArea显示最小值、平均值、最大值
开发语言·c#
wearegogog12312 小时前
C#与Twincat 2 实现上位机控制软PLC功能
开发语言·c#
军训猫猫头13 小时前
12.NModbus4在C#上的部署与使用 C#例子 WPF例子
开发语言·c#·wpf
Eiceblue13 小时前
使用 C# 设置 Excel 单元格格式
开发语言·后端·c#·.net·excel
LostXerxes14 小时前
C#的继承和多态
c#
Tiger_shl15 小时前
【.Net技术栈梳理】08-控制反转(IoC)与依赖注入(DI)
开发语言·.net·.netcore
Tiger_shl15 小时前
【.Net技术栈梳理】10-.NET Core 程序的执行
开发语言·.net·.netcore