Razor页面大文件上传导致400 Bad Request错误。

huangapple go评论144阅读模式
英文:

Razor Page Large File Upload resulting in 400 Bad Request

问题

我有一个用于上传文件的剃刀页面。我将最大上传大小设置为4GB,但当我上传大于2GB的文件时,出现400 Bad Request错误。我已经添加了互联网上可以找到的所有可能有助于最大上传大小的内容。有一段时间我以为错误已经消失了,但后来它又出现了,代码没有任何变化。

网站托管在IIS上。

有人知道可能是什么问题吗?我将包括所有与上传大小相关的代码。

附注:当我上传大于2GB但小于4GB的文件时,出现400 Bad Request错误。
当我上传大于4GB的文件时,出现413 Request Entity Too Large错误。

我在我的program.cs文件中有以下设置:

  1. builder.Services.Configure<FormOptions>(o =>
  2. {
  3. o.ValueLengthLimit = int.MaxValue;
  4. o.MultipartBodyLengthLimit = int.MaxValue;
  5. o.MultipartBoundaryLengthLimit = int.MaxValue;
  6. o.MultipartHeadersCountLimit = int.MaxValue;
  7. o.MultipartHeadersLengthLimit = int.MaxValue;
  8. o.BufferBodyLengthLimit = int.MaxValue;
  9. o.BufferBody = true;
  10. o.ValueCountLimit = int.MaxValue;
  11. });
  12. builder.Services.Configure<IISServerOptions>(options =>
  13. {
  14. options.MaxRequestBodySize = int.MaxValue;
  15. });
  16. builder.Services.Configure<KestrelServerOptions>(options =>
  17. {
  18. options.Limits.MaxRequestBodySize = int.MaxValue;
  19. });
  20. builder.WebHost.ConfigureKestrel(c =>
  21. {
  22. c.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(100);
  23. });

我在我的web.config文件中有以下设置:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration>
  3. <system.web>
  4. <!-- 大约2GB -->
  5. <httpRuntime maxRequestLength="419430412" executionTimeout="3600" /> <!-- kbytes -->
  6. <globalization
  7. requestEncoding="utf-8"
  8. responseEncoding="utf-8"
  9. />
  10. </system.web>
  11. <system.webServer>
  12. <security>
  13. <requestFiltering>
  14. <!-- 大约4GB -->
  15. <requestLimits maxAllowedContentLength="4294967295" /> <!-- bytes -->
  16. </requestFiltering>
  17. </security>
  18. </system.webServer>
  19. </configuration>

这是我的上传代码:

  1. [HttpPost]
  2. [RequestFormLimits(MultipartBodyLengthLimit = 4294967295)]
  3. [RequestSizeLimit(4294967295)]
  4. public async Task<IActionResult> OnPostAsync()
  5. {
  6. var files = Request.Form.Files;
  7. // 用于进度条
  8. var uploads = Path.Combine(_environment.WebRootPath, "uploads");
  9. var totalBytes = files.Sum(f => f.Length);
  10. //long bytesUploaded = 0;
  11. //var progressHandler = new ProgressHandler(_httpContextAccessor);
  12. // 文件夹名称
  13. var folderName = DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + " - " + HttpContext.Session.GetString("selectedResp");
  14. var folderPath = Path.Combine(@"C:\UploadTool\Uploads", folderName);
  15. if (!Directory.Exists(folderPath))
  16. {
  17. Directory.CreateDirectory(folderPath);
  18. if (files.Count == 0)
  19. {
  20. return BadRequest("未选择文件。");
  21. }
  22. foreach (var file in files)
  23. {
  24. var filePath = Path.Combine(folderPath, file.FileName);
  25. using (var stream = new FileStream(filePath, FileMode.Create))
  26. {
  27. await file.CopyToAsync(stream);
  28. }
  29. }
  30. }
  31. else
  32. {
  33. //ModelState.AddModelError(string.Empty, "文件夹已经存在!");
  34. }
  35. return RedirectToPage("Upload");
  36. }
英文:

I have a razor page for uploading files. I've set the maximum upload size to 4GB, but when I upload something larger than 2 GB I get an 400 Bad Request Error. I've added everything I found on the internet, that might help with the maximum upload size. At one point I thought the error was gone, but then it came back, with nothing changed in the code.
The Website is hosted on an IIS.

Has someone a hint what it might be? I'll include all upload size related code I've got.

Sidenote: When I upload a file > 2GB but < 4GB i get an 400 Bad Request Error.
When I upload a file larger than 4GB I get an 413 Request Entity Too Large Error.

I have these settings in my program.cs:

  1. builder.Services.Configure&lt;FormOptions&gt;(o =&gt;
  2. {
  3. o.ValueLengthLimit = int.MaxValue;
  4. o.MultipartBodyLengthLimit = int.MaxValue;
  5. o.MultipartBoundaryLengthLimit = int.MaxValue;
  6. o.MultipartHeadersCountLimit = int.MaxValue;
  7. o.MultipartHeadersLengthLimit = int.MaxValue;
  8. o.BufferBodyLengthLimit = int.MaxValue;
  9. o.BufferBody = true;
  10. o.ValueCountLimit = int.MaxValue;
  11. });
  12. builder.Services.Configure&lt;IISServerOptions&gt;(options =&gt;
  13. {
  14. options.MaxRequestBodySize = int.MaxValue;
  15. });
  16. builder.Services.Configure&lt;KestrelServerOptions&gt;(options =&gt;
  17. {
  18. options.Limits.MaxRequestBodySize = int.MaxValue;
  19. });
  20. builder.WebHost.ConfigureKestrel(c =&gt;
  21. {
  22. c.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(100);
  23. });

These settings in my web.config:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;configuration&gt;
  3. &lt;system.web&gt;
  4. &lt;!-- ~ 2GB --&gt;
  5. &lt;httpRuntime maxRequestLength=&quot;419430412&quot; executionTimeout=&quot;3600&quot; /&gt; // kbytes
  6. &lt;globalization
  7. requestEncoding=&quot;utf-8&quot;
  8. responseEncoding=&quot;utf-8&quot;
  9. /&gt;
  10. &lt;/system.web&gt;
  11. &lt;system.webServer&gt;
  12. &lt;security&gt;
  13. &lt;requestFiltering&gt;
  14. &lt;!-- ~ 4GB --&gt;
  15. &lt;requestLimits maxAllowedContentLength=&quot;4294967295&quot; /&gt; // bytes
  16. &lt;/requestFiltering&gt;
  17. &lt;/security&gt;
  18. &lt;/system.webServer&gt;
  19. &lt;/configuration&gt;

And this is the upload code:

  1. [HttpPost]
  2. [RequestFormLimits(MultipartBodyLengthLimit = 4294967295)]
  3. [RequestSizeLimit(4294967295)]
  4. public async Task&lt;IActionResult&gt; OnPostAsync()
  5. {
  6. var files = Request.Form.Files;
  7. // F&#252;r Progress Bar
  8. var uploads = Path.Combine(_environment.WebRootPath, &quot;uploads&quot;);
  9. var totalBytes = files.Sum(f =&gt; f.Length);
  10. //long bytesUploaded = 0;
  11. //var progressHandler = new ProgressHandler(_httpContextAccessor);
  12. // Folder Name
  13. var folderName = DateTime.Now.ToString(&quot;yyyy-MM-dd-HHmmss&quot;) + &quot; - &quot; + HttpContext.Session.GetString(&quot;selectedResp&quot;);
  14. var folderPath = Path.Combine(@&quot;C:\UploadTool\Uploads&quot;, folderName);
  15. if (!Directory.Exists(folderPath))
  16. {
  17. Directory.CreateDirectory(folderPath);
  18. if (files.Count == 0)
  19. {
  20. return BadRequest(&quot;No files selected.&quot;);
  21. }
  22. foreach (var file in files)
  23. {
  24. var filePath = Path.Combine(folderPath, file.FileName);
  25. using (var stream = new FileStream(filePath, FileMode.Create))
  26. {
  27. await file.CopyToAsync(stream);
  28. }
  29. }
  30. }
  31. else
  32. {
  33. //ModelState.AddModelError(string.Empty, &quot;Folder already exists!&quot;);
  34. }
  35. return RedirectToPage(&quot;Upload&quot;);
  36. }

答案1

得分: 1

这解决了问题。感谢 @pcalkins

  1. builder.Services.Configure<FormOptions>(o =>
  2. {
  3. o.ValueLengthLimit = int.MaxValue;
  4. o.MultipartBodyLengthLimit = 4L * 1024L * 1024L * 1024L;
  5. o.MultipartBoundaryLengthLimit = int.MaxValue;
  6. o.MultipartHeadersCountLimit = int.MaxValue;
  7. o.MultipartHeadersLengthLimit = int.MaxValue;
  8. o.BufferBodyLengthLimit = 4L * 1024L * 1024L * 1024L;
  9. o.BufferBody = true;
  10. o.ValueCountLimit = int.MaxValue;
  11. });
  12. builder.Services.Configure<IISServerOptions>(options =>
  13. {
  14. options.MaxRequestBodySize = Int64.MaxValue;
  15. });
  16. builder.Services.Configure<KestrelServerOptions>(options =>
  17. {
  18. options.Limits.MaxRequestBodySize = Int64.MaxValue;
  19. });
英文:

This fixed the problem. Thanks to @pcalkins

  1. builder.Services.Configure&lt;FormOptions&gt;(o =&gt;
  2. {
  3. o.ValueLengthLimit = int.MaxValue;
  4. o.MultipartBodyLengthLimit = 4L * 1024L * 1024L * 1024L;
  5. o.MultipartBoundaryLengthLimit = int.MaxValue;
  6. o.MultipartHeadersCountLimit = int.MaxValue;
  7. o.MultipartHeadersLengthLimit = int.MaxValue;
  8. o.BufferBodyLengthLimit = 4L * 1024L * 1024L * 1024L;
  9. o.BufferBody = true;
  10. o.ValueCountLimit = int.MaxValue;
  11. });
  12. builder.Services.Configure&lt;IISServerOptions&gt;(options =&gt;
  13. {
  14. options.MaxRequestBodySize = Int64.MaxValue;
  15. });
  16. builder.Services.Configure&lt;KestrelServerOptions&gt;(options =&gt;
  17. {
  18. options.Limits.MaxRequestBodySize = Int64.MaxValue;
  19. });

huangapple
  • 本文由 发表于 2023年8月10日 15:46:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定