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:
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:
Click on the download file link:
Your file will be downloaded.
En Kamran
11-Mar-2018Hello.
Thanks man.
I have a question:
How about downloading this file from other servers? in your example file is on local server and in my project i upload files on a download server, how can i download files from other server and i want to user doesnt see original download link.
And another thing, i am using ASP.NET Core MVC
Pravesh Singh
24-Sep-2014