Change file upload limits for an ASP.NET Core 3.1 App using Azure App Service
Glad @jimd12, after our discussion we came to know that your issue got resolved.
You found the discrepancy in the placement of the
web.config
file and placed it in thewwwroot
directory, which you misplaced in the Project directory, and then the required file size upload worked.
Posting as an answer so it will help if anyone facing this similar issue. even if you have allowed the content length to high value, please check the placement of web.config
file and refer to this SO thread that contains a few workarounds of overcoming the issue of file upload limits for Azure App Service.
How to increase the max upload file size in ASP.NET?
This setting goes in your web.config file. It affects the entire application, though... I don't think you can set it per page.
<configuration>
<system.web>
<httpRuntime maxRequestLength="xxx" />
</system.web>
</configuration>
"xxx" is in KB. The default is 4096 (= 4 MB).
Set minimum and maximum file size For file upload in asp.net core
In my opinion, if you want to check the minimum and maximum file size For file upload, I suggest you could try to create a custom middleware to check the httpcontentlength, if the http content length doesn't match the minimum and maximum size, then you could return the custom response.
More details, you could refer to below codes:
Add below middleware into the startup.cs Configure method:
Notice: I use app.usewhen to check the path, this will only work for the url path contains "api". If you want to match all the request, you could directly use app.Use
.
app.UseWhen(context =>
context.Request.Path.StartsWithSegments("/api"),
CheckRequestLengthAsync);
CheckRequestLengthAsync method:
private void CheckRequestLengthAsync(IApplicationBuilder app)
{
app.Use(async (context, next) =>
{
if (context.Request.ContentLength <50 && context.Request.ContentLength > 5)
{
context.Response.StatusCode = 500;
context.Response.ContentType = "text/html";
await context.Response.WriteAsync("Not match the content length");
}
else
{
// Do work that doesn't write to the Response.
await next();
// Do other work that doesn't write to the Response.
}
});
}
Result:
How to increase or set the upload media file size unlimited in ASP.NET Core 2.2 project
I just solved the problem with the help of my colleague. Thanks to all of you. because you participate.
I just added web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<httpRuntime maxRequestLength="1048576" />
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
</configuration>
I also added
[HttpPost]
[DisableRequestSizeLimit]
public IActionResult Upload([FromForm] IFormFile media)
{
if (media != null)
{
new HomeController().Run(media).Wait();
return Ok(media.FileName);
}
return NotFound();
}
How to increase the limit on the size of the uploaded file?
Have you tried to decorate the controller action
with [RequestSizeLimit(YOUR_MAX_TOTAL_UPLOAD_SIZE)]
along with the changes in Startup.cs
?
services.Configure<FormOptions>(opt =>
{
opt.MultipartBodyLengthLimit = YOUR_MAX_TOTAL_UPLOAD_SIZE;
});
btw, if you are planning to host the app on IIS, you can add web.config
file to your project and configure the upload size
there, rather than configuring it on the IIS server.
EDIT: as @XelaNimed's comment, adding the web.config
file along with editing the startup.cs
, got the code working.
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout">
<environmentVariables />
</aspNetCore>
</system.webServer>
</location>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="YOUR_BYTES" />
</requestFiltering>
</security>
</system.webServer>
</configuration>
Related Topics
Get Property Value from String Using Reflection
How to Deserialize Xml Document
Proper Use of the Idisposable Interface
What Are Some Good .Net Profilers
Difference Between a Reference Type and Value Type in C#
Difference Between String and String in C#
Deserializing Polymorphic Json Classes Without Type Information Using Json.Net
How to Calculate Someone'S Age Based on a Datetime Type Birthday
Finding the Variable Name Passed to a Function
How to Enable External Request in Iis Express
How to Start a Process from C#
Using Resources Folder in Unity