.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
});
相关推荐
EIP低代码平台8 小时前
EIP低代码平台-系统参数配置详解|落地三级等保+高灵活私有化部署方案
低代码·c#·权限·工作流·netcore
王莎莎-MinerU9 小时前
MCP 解决的是工具接入,科研 Agent 还缺的是科学证据接口标准化
开发语言·网络·人工智能·深度学习·pdf·c#·php
-银雾鸢尾-9 小时前
C#中Object类内的方法
开发语言·c#
tiankong121311 小时前
如何拓展多态下的子类
设计模式·c#
geats人山人海12 小时前
c# 第九章 record
开发语言·c#
-银雾鸢尾-12 小时前
C#中的抽象类与抽象方法
开发语言·c#
影寂ldy13 小时前
C# Task 进阶:WaitAll / WaitAny / WhenAll / WhenAny
开发语言·c#
咏方舟【长江支流】14 小时前
【编程】(1)-跨平台跨语言C#、Java、鸿蒙ArkTS三端通用实体接口
java·c#·ai编程·harmonyos·arkts·咏方舟-长江支流·用宝框架
o52788318414 小时前
校园一体化管理系统2.1、基于整洁四层架构 + DDD+CQRS 项目目录解读
后端·架构·c#·asp.net·visual studio