【问题标题】:Unable to upload file size larger than 200 MB in ASP.NET Core Web API无法在 ASP.NET Core Web API 中上传大于 200 MB 的文件
【发布时间】:2020-12-05 22:04:59
【问题描述】:

我开发了一个 ASP.NET 核心 web api 来将文件从一个地方上传到另一个地方。我使用邮递员测试 API。默认情况下,ASP.NET 核心在上传文件时最多接受 28 MB。对于 IIS express 方法,我在 wev.config 文件中增加了 maxAllowedContentLength 的大小,对于 Kestrel 方法,我增加了 MaxRequestBodySize 的大小。但是所有这些方法在文件大小达到 200 MB 时都可以正常工作,但是上传超过 200 MB 的文件会失败,甚至 [DisableRequestSizeLimit] 也会失败。我将 maxAllowedContentLength 和 MaxRequestBodySize 的值设置为超过 1 gb。请建议我在 ASP.NET Core Web api 中上传超过 1 gb 的大文件的方法。任何帮助表示赞赏。

IIS Express:

 <requestFiltering>
        <!-- Measured in Bytes -->
        <requestLimits maxAllowedContentLength="1073741824"  />
        <!-- 1 GB-->
      </requestFiltering>

红隼:

 public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.ConfigureKestrel((context, options) =>
                    {
                        options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(120);

                        options.Limits.RequestHeadersTimeout = TimeSpan.FromMinutes(120);
                        options.Limits.MaxRequestBodySize = 5242880000;
                       
                    webBuilder.UseStartup<Startup>();
                });

 [DisableRequestSizeLimit]
        [HttpPost]
        public  IActionResult PostUploadFiles([FromForm] List<IFormFile> postedFiles)
        {
            try
            {
                string wwwPath = this.Environment.WebRootPath;
                string contentPath = this.Environment.ContentRootPath;


                string path = Path.Combine(this.Environment.ContentRootPath, "Uploads");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                List<string> uploadedFiles = new List<string>();
                foreach (IFormFile postedFile in postedFiles)
                {
                    string fileName = Path.GetFileName(postedFile.FileName);
                    using (FileStream stream = new FileStream(Path.Combine(path, fileName), FileMode.Create))
                    {

                        postedFile.CopyTo(stream);
                        FileInfo file = new FileInfo(Path.Combine(path, fileName));
                        uploadedFiles.Add(fileName);
                      

                    }
                }

                Artifactory ar = new Artifactory();
                ar.FileName = "Sample";
                ar.Id = 1;
                ar.FileSize = 1024;
                string jsonConverted = JsonConvert.SerializeObject(ar);

                return new ObjectResult("File has  been uploaded") { StatusCode = Convert.ToInt32(HttpStatusCode.Created) };
            }
            catch (Exception ex)
            {
                return new ObjectResult(postedFiles) { StatusCode = Convert.ToInt32(HttpStatusCode.BadRequest) };
            }



        }

【问题讨论】:

  • 尝试添加:services.Configure&lt;FormOptions&gt;(x =&gt; x.MultipartBodyLengthLimit = &lt;value&gt;});
  • @Andy-我添加了你的建议,效果很好。services.Configure(x => x.MultipartBodyLengthLimit = });我上传了一个大小为 724 MB 的文件,它工作正常。感谢您的帮助。
  • @Andy-您能建议一个解决方案来解决 IIS Express 中的这个问题吗?
  • @Andy- 我尝试了stackoverflow.com/questions/53829487/… 上提到的 IIS 解决方案。但它没有用。

标签: c# asp.net-core asp.net-web-api postman


【解决方案1】:

对于 IIS,除了在请求过滤中设置 maxAllowedContentLength。 你还需要这样设置: 如果要取消文件上传限制,可以设置为null。

services.Configure<IISServerOptions>(options =>
            {
                options.MaxRequestBodySize = xxx;
            });

如果您在 IIS 中运行您的服务器,除了设置 MaxRequestBodySize 属性外,您还必须删除请求过滤模块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 2020-12-22
    • 2021-05-27
    • 2020-05-23
    • 2016-11-14
    • 2017-05-13
    • 1970-01-01
    相关资源
    最近更新 更多