banner



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.

ASP.NET MVC5 - Limit Upload File Type Extensions via Custom Data Annotation/Attribute

Prerequisites

Post-obit are some prerequisites earlier you proceed any further in this tutorial.

  1. Cognition of ASP.NET MVC5.
  2. Cognition of HTML.
  3. Knowledge of Bootstrap.
  4. 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.

  1.   .....
  2. < system.web >
  3. < authentication mode = "None" />
  4. < compilation debug = "truthful" targetFramework = "4.five.2" />
  5. < httpRuntime targetFramework = "4.v.2" executionTimeout = "108000" maxRequestLength = "1073741824" />
  6. </ system.web >
  7. < arrangement.webServer >
  8. < security >
  9. < requestFiltering >
  10. < requestLimits maxAllowedContentLength = "1073741824" />
  11. </ requestFiltering >
  12. </ security >
  13.   .....
  14. </ system.webServer >
  15.   .....

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.

  1. <!DOCTYPE html >
  2. < html >
  3. < head >
  4. < meta charset = "utf-8" />
  5. < meta name = "viewport" content = "width=device-width, initial-scale=i.0" >
  6. < title > @ViewBag.Title </ title >
  7.     @Styles.Return("~/Content/css")
  8.     @Scripts.Return("~/bundles/modernizr")
  9. < link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-crawly.min.css" />
  10. </ head >
  11. < trunk >
  12. < div form = "navbar navbar-inverse navbar-fixed-top" >
  13. < div class = "container" >
  14. < div course = "navbar-header" >
  15. < button type = "button" class = "navbar-toggle" information-toggle = "collapse" data-target = ".navbar-collapse" >
  16. < span class = "icon-bar" > </ span >
  17. < span class = "icon-bar" > </ bridge >
  18. < bridge class = "icon-bar" > </ span >
  19. </ button >
  20. </ div >
  21. </ div >
  22. </ div >
  23. < div class = "container body-content" >
  24.         @RenderBody()
  25. < 60 minutes />
  26. < footer >
  27. < eye >
  28. < p > < strong > Copyright © @DateTime.Now.Year - < a href = "http://wwww.asmak9.com/" > Asma's 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 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.

  1. namespace  ImgExtLimit.Helper_Code.Common
  2. {
  3. using  Organisation;
  4. using  System.Collections.Generic;
  5. using  Organisation.ComponentModel.DataAnnotations;
  6. using  System.Linq;
  7. using  System.Spider web;
  8.     [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple =false , Inherited = true )]
  9. public class  AllowExtensionsAttribute : ValidationAttribute
  10.     {
  11.         #region Public / Protected Properties
  12. public string  Extensions { become ; fix ; } = "png,jpg,jpeg,gif" ;
  13.         

0 Response to "How To Custom Upload File Only"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel