How to Increase the max upload file size in ASP.NET MVC
By FoxLearn 2/18/2024 1:23:33 AM 205
By default, you only upload up to 4MB (4096 KB) when hosting your website on IIS.
To increase file size when uploading on IIS 7 you need to open the web.config file, then add the configuration as below.
<system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="52428800" /> </requestFiltering> </security> </system.webServer>
As you can see, I have increased the max file upload size to 50MB. You can also increase the maximun upload file size by creating a custom file size attribute as shown below.
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Web; using System.Web.Mvc; namespace Customs { [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public class FileSizeAttribute : ValidationAttribute, IClientValidatable { public int? MaxBytes { get; set; } public FileSizeAttribute(int maxBytes) : base("Please upload a supported file.") { MaxBytes = maxBytes; if (MaxBytes.HasValue) ErrorMessage = $"Please upload a file of less than {MaxBytes.Value} bytes."; } public override bool IsValid(object value) { HttpPostedFileBase file = value as HttpPostedFileBase; if (file != null) { bool result = true; if (MaxBytes.HasValue) result &= (file.ContentLength < MaxBytes.Value); return result; } return true; } IEnumerable<ModelClientValidationRule> IClientValidatable.GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { var rule = new ModelClientValidationRule { ValidationType = "filesize", ErrorMessage = FormatErrorMessage(metadata.DisplayName) }; rule.ValidationParameters["maxbytes"] = MaxBytes; yield return rule; } } }
Open the FilterConfig.cs file, then add FileSizeAttribute to the RegisterGlobalFilters method that lets you filter the upload file size to IIS.
using System.Web; using System.Web.Mvc; namespace CSharpCode { public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new FileSizeAttribute()); } } }
- Unable to resolve service for type 'Microsoft.AspNetCore.Identity.RoleManager'
- ASP.NET MVC Responsive Templates Free Download
- How to upload file in ASP.NET MVC
- How to Create Contact Form Flat Responsive in ASP.NET MVC
- HTTP Error 500.30 ASP.NET Core app failed to start
- How to Use IExceptionHandler in ASP.NET Core
- How to custom exception handling in ASP.NET Core
- How to check if HttpPostedFileBase is an image
Categories
Popular Posts
Plus Admin Dashboard Template
11/18/2024
HTML Login Form
11/11/2024
SB Admin Template
11/17/2024
Stisla Admin Dashboard Template
11/18/2024