.Net 6 上传文件接口 文件大小报错整体配置

csharp 复制代码
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("UploadifyFile")]
        [RequestSizeLimit(2000 * 1024 * 1024)] // 设置最大请求体大小为 100MB
        public async Task<IActionResult> UploadifyFile(IFormFile file)
        {
            try
            {
                // 文件上传逻辑
                var filePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\attachments", file.FileName);
                // 获取当前工作目录
                string extractPath = Directory.GetCurrentDirectory() + @"\wwwroot\attachments";
                if (!System.IO.File.Exists(filePath))
                {
                    await SaveFileAsync(file, filePath);
                    //if (IsCompressedFile(filePath))
                    //{
                    // 解压文件到当前目录
                    ExtractArchive(filePath, extractPath);
                    System.IO.File.Delete(filePath);
                    //}
                }
                return Ok(filePath);
            }
            catch (Exception ex)
            {

                // 日志记录异常信息
                _logger.LogError(ex, "An error occurred during file upload.");
                return Ok(ex.Message);
            }


        }
csharp 复制代码
var builder = WebApplication.CreateBuilder(args);
//修改FormOptions(用于将读取请求正文配置为 HTTP 窗体的选项)配置
builder.Services.Configure<FormOptions>(options =>
{
    options.MultipartBodyLengthLimit = int.MaxValue; //每个多部分主体的长度限制,默认值约为128MB 当前为2G
    options.ValueCountLimit = int.MaxValue; //要允许的表单条目数限制,默认为 1024个 当前为2147483647个
    options.ValueLengthLimit = int.MaxValue; //单个窗体值的长度限制 大约为 4MB 当前为2G
});

//为 ASP.NET Core Kestrel Web 服务器配置选项
builder.Services.Configure<KestrelServerOptions>(options =>
{
    options.Limits.MaxRequestBodySize = int.MaxValue; // 默认大约为 28.6MB 当前为2G
});

//为 IIS 进程内提供配置
builder.Services.Configure<IISServerOptions>(options =>
{
    options.MaxRequestBodySize = int.MaxValue; // 默认大约为 28.6MB 当前为2G
});
相关推荐
电商api接口开发6 分钟前
ASP.NET MVC 入门与提高指南六
c#·asp.net·mvc
宝桥南山16 分钟前
Azure Devops - 尝试一下在Pipeline中使用Self-hosted Windows agent
microsoft·微软·c#·.netcore·azure·devops
冰茶_8 小时前
WPF之TextBox控件详解
学习·microsoft·微软·c#·wpf
大飞pkz12 小时前
【Unity】使用XML进行数据读存的简单例子
xml·unity·c#·游戏引擎·游戏开发·数据读写
编程乐趣17 小时前
基于C#开发的适合Windows开源文件管理器
开发语言·windows·c#
code_shenbing19 小时前
C# 实现列式存储数据
开发语言·c#·存储
code_shenbing19 小时前
.NET Core 数据库ORM框架用法简述
数据库·c#·.netcore·orm
ghost14321 小时前
C#学习第20天:垃圾回收
开发语言·学习·c#
code_shenbing1 天前
WPF实现类似Microsoft Visual Studio2022界面效果及动态生成界面技术
ui·c#·wpf·上位机
code_shenbing1 天前
C# 高效操作excel文件
c#·excel·图表