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

        /// <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);
            }


        }

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
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值