Home > DeveloperSection > Articles > Download File in Asp.Net Mvc 4

Download File in Asp.Net Mvc 4


ASP.NET MVC ASP.NET MVC 
Ratings:
2 Comment(s)
 8342  View(s)
Rate this:

Download File in Asp.Net Mvc 4

In this article, I’m explaining how to implement the download file concept in asp.net mvc 4.

Step 1

First create a basic asp.net mvc 4 application and add a controller named “HomeController” to it:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace DownloadFileMvcApp.Controllers

{

    public class HomeController : Controller

    {

        //

        // GET: /Home/

 

        public ActionResult Index()

        {

            return View();

        }

 

       

        public FileResult downloadFile()

        {

            return new FilePathResult(Server.MapPath("/File/dummy.docx"), "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

        }

 

    }

}

 

Step 2

Now add a folder to the project named “File” and add a word file to it:

Download File in Asp.Net Mvc 4

Step 3

Now add a view named “Index” to the project:

@{

    ViewBag.Title = "Index";

}

 

<h2>Download File Sample</h2>

 

<div>

    @Html.ActionLink("Download File""downloadFile""Home")

</div>

 

Output

Now run the application:

Download File in Asp.Net Mvc 4

Click on the download file link:

Download File in Asp.Net Mvc 4

Your file will be downloaded.


Nice Article

By Pravesh Singh on   2 years ago
Nice Article

good article

By Pravesh Singh on   2 years ago
good article

Don't want to miss updates? Please click the below button!

Follow MindStick