Upload Image Bigger Than 5 Mb to Sql Server
The size of an uploaded file is an of import factor for considering the storage capacity. We cannot allow an infinite file size to be uploaded by the finish-users considering this volition crusade only one user to fill the server storage chapters in the worst case scenario. Well, that could crusade a lot of issues at the dorsum-end server. So, restricting or limiting the upload file size is i of the fundamental business organization requirements of the spider web application. Sometimes, simply image files are accepted by the web application, sometimes just documents, and sometimes the combination of image, documents, and compressed file types are accepted by the web arrangement.
Today, I shall be demonstrating limiting/restricting of upload file size past implementing a custom data annotation/aspect component on ASP.Net MVC5 platform. This article is not specific to image files but, you tin use the provided solution with any blazon of file format also.
Prerequisites
Following are some prerequisites before yous proceed any further in this tutorial.
- Knowledge of ASP.Cyberspace MVC5
- Knowledge of HTML
- Knowledge of Bootstrap
- Knowledge of C# Programming
You can download the complete source code for this tutorial or you tin can follow the stride by stride discussion below. The sample code is existence adult in Microsoft Visual Studio 2015 Enterprise.
Allow'due south begin now.
Step 1
Create a new MVC web project and proper noun information technology "ImgFileSizeLimit".
Step 2
You demand to add together/update the values of "executionTimeout", "maxRequestLength" & "maxAllowedContentLength" properties if not already added in the "Web.config" file, equally shown beneath.
- .....
- < system.web >
- < authentication mode = "None" />
- < compilation debug = "true" targetFramework = "4.five.2" />
- < httpRuntime targetFramework = "4.five.ii" executionTimeout = "108000" maxRequestLength = "1073741824" />
- </ organization.web >
- < organization.webServer >
- < security >
- < requestFiltering >
- < requestLimits maxAllowedContentLength = "1073741824" />
- </ requestFiltering >
- </ security >
- .....
- </ system.webServer >
- .....
executionTimeout -> The amount of time required to procedure your asking on the web server; The value is provided in seconds.
maxRequestLength -> The maximum size which your request can capture and send to the web server; The value is provided in bytes.
maxAllowedContentLength -> The maximum allowed size of your content (e.m. file, text data etc) to be sent to the web server; The value is provided in bytes.
Stride 3
Open the "Views->Shared->_Layout.cshtml" file and supersede the lawmaking with the following code in it.
- <!DOCTYPE html >
- < html >
- < head >
- < meta charset = "utf-8" />
- < meta name = "viewport" content = "width=device-width, initial-scale=one.0" >
- < title > @ViewBag.Title </ title >
- @Styles.Render("~/Content/css")
- @Scripts.Render("~/bundles/modernizr")
- < link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/font-awesome/iv.four.0/css/font-awesome.min.css" />
- </ head >
- < body >
- < div class = "navbar navbar-inverse navbar-fixed-top" >
- < div class = "container" >
- < div class = "navbar-header" >
- < button type = "button" class = "navbar-toggle" data-toggle = "collapse" data-target = ".navbar-collapse" >
- < span form = "icon-bar" > </ bridge >
- < bridge class = "icon-bar" > </ span >
- < span class = "icon-bar" > </ span >
- </ push button >
- </ div >
- </ div >
- </ div >
- < div class = "container torso-content" >
- @RenderBody()
- < hr />
- < footer >
- < heart >
- < p > < strong > Copyright © @DateTime.At present.Twelvemonth - < a href = "http://wwww.asmak9.com/" > Asma's Web log </ a > . </ stiff > All rights reserved. </ p >
- </ centre >
- </ footer >
- </ div >
- @*Scripts*@
- @Scripts.Return("~/bundles/jquery")
- @Scripts.Render("~/bundles/jqueryval")
- @Scripts.Return("~/bundles/bootstrap")
- @RenderSection("scripts", required: false)
- </ body >
- </ html >
In the higher up lawmaking, I take only created a basic default layout page and linked the crave libraries into it.
Step 4
Create a new "Helper_Code\Common\AllowFileSizeAttribute.cs" file and paste the following code in it.
- namespace ImgFileSizeLimit.Helper_Code.Mutual
- {
- using System;
- using Arrangement.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using Organisation.Web;
- [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple =imitation , Inherited = truthful )]
- public grade AllowFileSizeAttribute : ValidationAttribute
- {
- #region Public / Protected Properties
- public int FileSize { get ; set ; } = 1 * 1024 * 1024 * 1024;
Post a Comment for "Upload Image Bigger Than 5 Mb to Sql Server"