How To Custom Upload File Only
Restricting or limiting the file blazon extensions is a fundamental business requirement. It is not necessary that a business organization allows all file types to be uploaded via their web awarding. Sometimes, only image files are accustomed by the web application, sometimes merely documents, and sometimes the combination of image, documents, and compressed file types are accepted past the web system.
Today, I shall be demonstrating the process of limiting/restricting the desired upload file type extensions past implementing custom information annotation/attribute component on ASP.Net MVC5 platform. This article is not specific to image files simply, you can use the provided solution with any type of file format every bit well.
Prerequisites
Post-obit are some prerequisites earlier you proceed any further in this tutorial.
- Cognition of ASP.NET MVC5.
- Cognition of HTML.
- Knowledge of Bootstrap.
- Cognition of C# Programming.
You can download the consummate source code for this tutorial or yous can follow the stride by step discussion below. The sample code is being developed in Microsoft Visual Studio 2015 Enterprise.
Let's begin now.
Footstep i
Create a new MVC web project and name it as "ImgExtLimit".
Step 2
You demand to add/update the "executionTimeout", "maxRequestLength", and "maxAllowedContentLength" belongings values if not already added in the "Web.config" file, as shown beneath.
- .....
- < system.web >
- < authentication mode = "None" />
- < compilation debug = "truthful" targetFramework = "4.five.2" />
- < httpRuntime targetFramework = "4.v.2" executionTimeout = "108000" maxRequestLength = "1073741824" />
- </ system.web >
- < arrangement.webServer >
- < security >
- < requestFiltering >
- < requestLimits maxAllowedContentLength = "1073741824" />
- </ requestFiltering >
- </ security >
- .....
- </ system.webServer >
- .....
executionTimeout -> Amount of fourth dimension required to process your request on the web server. The value is provided in seconds.
maxRequestLength -> Maximum size which your request can capture and send to the web server. The value is provided in bytes.
maxAllowedContentLength -> Maximum allowed size of your content (e.yard. file, text data etc.) that is sent to the web server. The value is provided in bytes.
Step 3
Open the "Views->Shared->_Layout.cshtml" file and supervene upon the code with the post-obit.
- <!DOCTYPE html >
- < html >
- < head >
- < meta charset = "utf-8" />
- < meta name = "viewport" content = "width=device-width, initial-scale=i.0" >
- < title > @ViewBag.Title </ title >
- @Styles.Return("~/Content/css")
- @Scripts.Return("~/bundles/modernizr")
- < link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-crawly.min.css" />
- </ head >
- < trunk >
- < div form = "navbar navbar-inverse navbar-fixed-top" >
- < div class = "container" >
- < div course = "navbar-header" >
- < button type = "button" class = "navbar-toggle" information-toggle = "collapse" data-target = ".navbar-collapse" >
- < span class = "icon-bar" > </ span >
- < span class = "icon-bar" > </ bridge >
- < bridge class = "icon-bar" > </ span >
- </ button >
- </ div >
- </ div >
- </ div >
- < div class = "container body-content" >
- @RenderBody()
- < 60 minutes />
- < footer >
- < eye >
- < p > < strong > Copyright © @DateTime.Now.Year - < a href = "http://wwww.asmak9.com/" > Asma's Blog </ a > . </ strong > All rights reserved. </ p >
- </ center >
- </ footer >
- </ div >
- @*Scripts*@
- @Scripts.Render("~/bundles/jquery")
- @Scripts.Render("~/bundles/jqueryval")
- @Scripts.Render("~/bundles/bootstrap")
- @RenderSection("scripts", required: false)
- </ body >
- </ html >
In the in a higher place lawmaking, I have simply created a basic default layout page and linked the require libraries into it.
Pace 4
Create a new "Helper_Code\Mutual\AllowExtensionsAttribute.cs" file and add the following code.
- namespace ImgExtLimit.Helper_Code.Common
- {
- using Organisation;
- using System.Collections.Generic;
- using Organisation.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Spider web;
- [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple =false , Inherited = true )]
- public class AllowExtensionsAttribute : ValidationAttribute
- {
- #region Public / Protected Properties
- public string Extensions { become ; fix ; } = "png,jpg,jpeg,gif" ;
0 Response to "How To Custom Upload File Only"
Post a Comment