When I built a Web API to allow the client to upload big files (more than 100 Mb), I got an error:
Multipart body length limit 16384 exceeded
I referred to links, which can be found here linkA and here linkB.
Based on suggestions, I first created a simple controller with .NET Core 3.1.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
namespace TestFormDataUpload.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class FileController : ControllerBase
{
[HttpPost("upload"), DisableRequestSizeLimit]
public async Task<ActionResult<string>> UploadZipperAsync([FromForm] IFormCollection uploadRequest)
{
return Ok(new string("upload successfully"));
}
}
}
I also configure the Kestrel server in the Program
file, and FormOptions
in the Startup
file.
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseKestrel(options => { options.Limits.MaxRequestBodySize = null; });
});
public void ConfigureServices(IServiceCollection services)
{
services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.ValueCountLimit = int.MaxValue;
x.MultipartBodyLengthLimit = long.MaxValue;
x.MultipartBoundaryLengthLimit = int.MaxValue;
x.BufferBodyLengthLimit = long.MaxValue;
x.BufferBody = true;
x.MemoryBufferThreshold = int.MaxValue;
x.KeyLengthLimit = int.MaxValue;
x.MultipartHeadersLengthLimit = int.MaxValue;
x.MultipartHeadersCountLimit = int.MaxValue;
});
services.AddControllers();
}
But when I call the upload
API in the Postman, the error is still reported.
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|c4a88f96-4f36a455f5afbcb5.",
"errors": {
"": [
"Failed to read the request form. Multipart body length limit 16384 exceeded."
]
}
}
My curl command is:
curl --location --request POST 'http://localhost:5000/api/file/upload' \
--form 'uploadedFile=@"/C:/Users/xxx/Repos/RicardoIsDebugging/netcore-trial/file-operation/ZipperClient/bin/Debug/netcoreapp3.1/tempfile.7z"'
Can any one show me any feasibly solution? Better with code examples:)