• How to upload files in ASP.NET MVC 4 with restriction or specific extension?

    by  • October 6, 2015 • Microsoft .NET Framework, Microsoft ASP.NET MVC • 0 Comments

    Introduction:

    File uploading is a common feature in web based projects. This article explains how to upload any file, how to save file in project directory and how to upload file with restriction. When we upload a file in ASP.NET MVC 4 only controller and view interacts with each other.

    Here we shall explain file uploading approach. Let’s take a look at ASP.NET MVC 4 file upload in general.

    1) View.cshtml:

    This approach is very simple and it is easy way to upload file. The form method is POST, and the form encoding type is “multipart/form-data”. These parameters are required for uploading binary data to the server.

    Controller.cs

    The action receives the uploaded file from view. HttpPostedFileBase object is used to access file that has passed from view using BeginForm() Method.

    In this block of code you can write any file extension before upload. Just you need to pass file extension in “if statement” to restrict any file with respect to format.

    Server.MapPath() is used to save  file in project directory. Complete code of controller is following:

     




    Namecheap.com
    Avatar of Abdul Rehman

    About

    Abdul Rehman writes on .NET technologies at GetCodeSnippet.com. GetCodeSnippet.com provides you high quality Microsoft .NET development related articles, tutorials and sample code for ASP.NET, C#, VB.NET, AJAX, WCF, WPF, Web Services, Windows Services, SQL Server and other technologies.

    http://getcodesnippet.com

    Leave a Reply