Skip to content Skip to sidebar Skip to footer

How to Set Maximum Size of File Uploads Asp.net Mvc

The size of an uploaded file is an of import factor for because the storage capacity. Nosotros cannot allow an infinite file size to exist uploaded past the end-users because this will crusade simply i user to fill the server storage capacity in the worst case scenario. Well, that could cause a lot of issues at the back-end server. And then, restricting or limiting the upload file size is one of the cardinal business requirements of the spider web application. Sometimes, only image files are accepted past the web awarding, sometimes only documents, and sometimes the combination of image, documents, and compressed file types are accustomed by the spider web system.

Today, I shall be demonstrating limiting/restricting of upload file size past implementing a custom data annotation/attribute component on ASP.NET MVC5 platform. This article is not specific to image files only, you can use the provided solution with whatsoever type of file format as well.

Prerequisites

Following are some prerequisites before yous proceed any further in this tutorial.

  1. Noesis of ASP.NET MVC5
  2. Noesis of HTML
  3. Knowledge of Bootstrap
  4. Knowledge of C# Programming

You tin download the complete source code for this tutorial or yous tin follow the step past footstep give-and-take beneath. The sample lawmaking is being developed in Microsoft Visual Studio 2015 Enterprise.

Let'southward begin now.

Step 1

Create a new MVC web project and name it "ImgFileSizeLimit".

Step ii

You demand to add/update the values of "executionTimeout", "maxRequestLength" & "maxAllowedContentLength" properties if non already added in the "Web.config" file, as shown below.

  1.   .....
  2. < system.web >
  3. < hallmark style = "None" />
  4. < compilation debug = "true" targetFramework = "4.v.2" />
  5. < httpRuntime targetFramework = "iv.5.two" executionTimeout = "108000" maxRequestLength = "1073741824" />
  6. </ system.web >
  7. < system.webServer >
  8. < security >
  9. < requestFiltering >
  10. < requestLimits maxAllowedContentLength = "1073741824" />
  11. </ requestFiltering >
  12. </ security >
  13.   .....
  14. </ system.webServer >
  15.   .....

executionTimeout -> The corporeality of time required to process your request on the web server; The value is provided in seconds.

maxRequestLength -> The maximum size which your asking can capture and send to the spider web server; The value is provided in bytes.

maxAllowedContentLength -> The maximum allowed size of your content (e.yard. file, text data etc) to be sent to the web server; The value is provided in bytes.

Footstep 3

Open the "Views->Shared->_Layout.cshtml" file and supersede the code with the post-obit code in it.

  1. <!DOCTYPE html >
  2. < html >
  3. < head >
  4. < meta charset = "utf-8" />
  5. < meta name = "viewport" content = "width=device-width, initial-calibration=1.0" >
  6. < title > @ViewBag.Championship </ title >
  7.     @Styles.Render("~/Content/css")
  8.     @Scripts.Render("~/bundles/modernizr")
  9. < link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/font-awesome/four.four.0/css/font-awesome.min.css" />
  10. </ head >
  11. < body >
  12. < div class = "navbar navbar-inverse navbar-fixed-top" >
  13. < div class = "container" >
  14. < div class = "navbar-header" >
  15. < button type = "button" class = "navbar-toggle" data-toggle = "collapse" data-target = ".navbar-collapse" >
  16. < span class = "icon-bar" > </ span >
  17. < span class = "icon-bar" > </ bridge >
  18. < span course = "icon-bar" > </ span >
  19. </ push >
  20. </ div >
  21. </ div >
  22. </ div >
  23. < div class = "container torso-content" >
  24.         @RenderBody()
  25. < hr />
  26. < footer >
  27. < center >
  28. < p > < strong > Copyright © @DateTime.Now.Year - < a href = "http://wwww.asmak9.com/" > Asma'southward Blog </ a > . </ strong >  All rights reserved. </ p >
  29. </ center >
  30. </ footer >
  31. </ div >
  32.     @*Scripts*@
  33.     @Scripts.Render("~/bundles/jquery")
  34.     @Scripts.Render("~/bundles/jqueryval")
  35.     @Scripts.Render("~/bundles/bootstrap")
  36.     @RenderSection("scripts", required: false)
  37. </ body >
  38. </ html >

In the above lawmaking, I accept but created a basic default layout page and linked the require libraries into it.

Step 4

Create a new "Helper_Code\Common\AllowFileSizeAttribute.cs" file and paste the following code in it.

  1. namespace  ImgFileSizeLimit.Helper_Code.Mutual
  2. {
  3. using  Organisation;
  4. using  System.Collections.Generic;
  5. using  Arrangement.ComponentModel.DataAnnotations;
  6. using  Arrangement.Linq;
  7. using  System.Spider web;
  8.     [AttributeUsage(AttributeTargets.Field | AttributeTargets.Holding, AllowMultiple =false , Inherited = true )]
  9. public course  AllowFileSizeAttribute : ValidationAttribute
  10.     {
  11.         #region Public / Protected Backdrop
  12. public int  FileSize { get ; set ; } = i * 1024 * 1024 * 1024;
  13.         

Post a Comment for "How to Set Maximum Size of File Uploads Asp.net Mvc"