I am creating a file upload with .NET Core 3.1.
When I uploaded a file larger than 100 MB, I got an error:
413 Request Entity Too Large
As a result of various investigations, I was able to avoid the 413 error by making the following settings:
web.config
:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<security>
<requestFiltering>
<!-- Handle requests up to 1 GB -->
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
</location>
</configuration>
UploadController.cs
:
[Route("embed")]
[HttpPost]
[DisableRequestSizeLimit]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public ActionResult PostEmbed([FromForm] WebPptVoiceEmbedRequest request)
{
Log.NLog.Debug("[Get]:PostEmbed start");
byte[] array;
using (var stream = new MemoryStream())
{
request.File.CopyTo(stream);
array = stream.ToArray();
}
Log.NLog.Debug("[Get]:PostEmbed end");
return File(array, "application/vnd.openxmlformats-officedocument.presentationml.presentation", request.File.FileName);
}
Program.cs
:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel(options =>
{
options.Limits.MaxRequestBodySize = null;
})
.UseStartup<Startup>();
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(LogLevel.Debug);
})
.UseNLog();
client.js
let embedRequest = new FormData();
embedRequest.append('file', file);
const { data } = await axios
.post(Api.embed, embedRequest, {
responseType: 'blob',
dataType: 'binary',
})
.catch((error) => {
apiErrorLogAndThrow(error);
});
const blob = new Blob([data]);
return blob;
However, I still get an error
400 (Bad Request
Is there a place to set the file upload capacity other than the place I set?
It's confusing because there are too many size settings in various places.
I want to solve it first,
If possible, I would like to know how to solve it without using web.config.
Best regards