Click here to Skip to main content
15,890,512 members
Articles / Programming Languages / C#
Tip/Trick

How To Download a File in MVC

Rate me:
Please Sign up or sign in to vote.
3.84/5 (7 votes)
9 Sep 2015CPOL1 min read 87.6K   10   12
Provide an easy way to download file in MVC

Introduction

File downloading is a kind of common functionality which we want in our web application. I already have a post http://www.codeproject.com/Tips/663277/File-Download-on-ImageButton-or-Button-Click which provides the code snippet to be used for this functionality in webforms.

But now the era has changed. Developers are switching to MVC now and so there comes a need for file downloading snippet. Mostly file downloading is provided in two ways. Files are saved in database (i.e., in form of bytes) or file is physically present on application server inside the application hierarchy. The following snippet works for both the scenarios as it uses the BYTES format to provide file as download.

Background

When I started work in MVC for the first time and when the file download task comes in, I had to Google for the solution. So, the purpose of posting this tip is to help the naive programmer like me to get the solution in the easiest way.

Using the Code

Following is a simple code snippet which can be used as Action in MVC Controller. It simply picks the file from one folder in application hierarchy, converts it into bytes and return the result as file to View.

C#
public ActionResult DownloadFile()
      {
          string path = AppDomain.CurrentDomain.BaseDirectory + "FolderName/";
          byte[] fileBytes = System.IO.File.ReadAllBytes(path + "filename.extension");
          string fileName = "filename.extension";
          return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
      }

Simply replace the FolderName with the folder in which file is present in project hierarchy, and replace filename.extension with your filename along with extension to make it work properly. Simply call this in view using the line below:

C#
@Html.ActionLink("Click here to download", "DownloadFile", new { })

Points of Interest

Many code snippets are available on Google, but I found this one pretty interesting and straight forward for any programmer who is a kind of newbie like myself.

Do give me your feedback, either suggestions, corrections or praises. Smile | :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Pakistan Pakistan
Working as an Associate Software Engineer in LMKT


Visit My Blog!

Contact Me!

Comments and Discussions

 
QuestionDOWNLOAD FILE MVC Pin
Member 147115099-Jan-20 1:35
Member 147115099-Jan-20 1:35 
Question[My vote of 1] Isn't this what MSDN is for? Pin
User 1013254611-Sep-15 0:11
User 1013254611-Sep-15 0:11 
AnswerRe: [My vote of 1] Isn't this what MSDN is for? Pin
VICK13-Sep-15 19:10
professional VICK13-Sep-15 19:10 
GeneralRe: [My vote of 1] Isn't this what MSDN is for? Pin
User 1013254613-Sep-15 22:39
User 1013254613-Sep-15 22:39 
GeneralRe: [My vote of 1] Isn't this what MSDN is for? Pin
VICK14-Sep-15 20:01
professional VICK14-Sep-15 20:01 
GeneralRe: [My vote of 1] Isn't this what MSDN is for? Pin
User 1013254614-Sep-15 20:44
User 1013254614-Sep-15 20:44 
QuestionRegarding code Pin
Tridip Bhattacharjee10-Sep-15 22:06
professionalTridip Bhattacharjee10-Sep-15 22:06 
AnswerRe: Regarding code Pin
VICK13-Sep-15 19:06
professional VICK13-Sep-15 19:06 
SuggestionReading the file into memory Pin
John Brett10-Sep-15 2:27
John Brett10-Sep-15 2:27 
GeneralRe: Reading the file into memory Pin
VICK10-Sep-15 2:40
professional VICK10-Sep-15 2:40 
GeneralRe: Reading the file into memory Pin
User 1013254611-Sep-15 0:07
User 1013254611-Sep-15 0:07 
GeneralRe: Reading the file into memory Pin
VICK13-Sep-15 19:11
professional VICK13-Sep-15 19:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.