.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
});
相关推荐
程序设计实验室13 小时前
分析C#项目的单元测试覆盖率,提高代码质量
c#
一顿操作猛如虎,啥也不是!16 小时前
c# 在 23:00 - 23:59 之间执行一次的写法
开发语言·c#
格林威17 小时前
Baumer相机如何通过YoloV8深度学习模型实现工厂自动化产线牛奶瓶盖实时装配的检测识别(C#代码UI界面版)
人工智能·深度学习·数码相机·yolo·机器学习·计算机视觉·c#
DataIntel18 小时前
c# 属性操作(2)
c#
白葵新19 小时前
C#案例实战
c++·python·算法·计算机视觉·c#
CodeCraft Studio1 天前
图像处理控件Aspose.Imaging教程:使用 C# 将 SVG 转换为 EMF
图像处理·microsoft·c#·svg·aspose·图片格式转换·emf
★YUI★1 天前
学习游戏制作记录(将各种属性应用于战斗以及实体的死亡)8.5
学习·游戏·unity·c#
jason成都1 天前
ubuntu编译opendds开发(C#)
linux·ubuntu·c#·opendds
小黄花呀小黄花1 天前
从零开始构建工业自动化软件框架:基础框架搭建(三)容器、配置、日志功能测试
c#
小黄花呀小黄花1 天前
从零开始构建工业自动化软件框架:基础框架搭建(一)容器与日志功能实现
c#