Download File in MVC

29 Oct 2013, 01:05 PM

The file downloading is the normal functionality in web development. In MVC some developer feels it is difficult to do it from controller. The downloading file in MVC is as simple as it is in ASP.Net. Suppose we have some byte, string or physical file on the server and we want to write method for downloading it. Write a download method in controller and use appropriate return type for it as per your requirement. In this blog we will use two techniques.

Download Bytes

Most of the time developer need to return bytes from the server side. Here is the code below that return file bytes in response. You may be familiar that there are different results that we return from the controller e.g. ActionResult, PartialViewResult etc. We return different objects based on our requirement. FileContentResult is used to return the file to the client.

 
 public class FileController : BaseController
 {
  
    [HttpGet]
    public FileContentResult DownloadFile(int id)
    {

        var item = dbManager.Get(id);

        if (!String.IsNullOrEmpty(item.XmlText))
        {
        
            HttpContext.Response.AddHeader("content-disposition",
                    "attachment; filename=" + item.FileName);

            var mimeType = "application/xml";

            byte[] fileContent = Encoding.ASCII.GetBytes(item.XmlText);
                    return File(fileContent, mimeType, item.FileName);

        }

        return null;

    }

 }
    

Return type of your method in controller should be FileContentResult. It is used for returning the bytes from controller. In this example I am converting bytes from strig using Encoding.ASCII.GetBytes(item.XmlText) method. You can use any other method or if you already have bytes then there is no need to use it.

You may be familiar that there are different results that we return from the controller e.g. ActionResult, PartialViewResult etc. We return different objects based on our requirement. FileContentResult is used to return the file to the client.

Download Physical File

Some time we have physical files on the server and we want to return the file as it is without converting into the bytes.

 [HttpGet]
 public FilePathResult DownloadFile(int id)
 {

    var item = _proxy.Get(id);

    HttpContext.Response.AddHeader("content-disposition",
                                    "attachment; filename=" + item.FileName);

    var filePath = Server.MapPath("~/content/test.xml");
    return File(filePath, "application/xml", item.FileName);

 }
    

The return type should be FilePathResult that indicates, the file is physically exists on server.

Both techniques create object using File() method of the controller class that returns the appropriate result object for controller. There are six overloaded methods and you can use one of them as per your requirement.

  • FileContentResult File(byte[] fileContents, string contentType);
  • FileStreamResult File(Stream fileStream, string contentType);
  • FilePathResult File(string fileName, string contentType);
  • FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName);
  • FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName);
  • FilePathResult File(string fileName, string contentType, string fileDownloadName);
About Blog
This article explains how to download file, bytes in MVC.

Date 29 Oct 2013, 01:00 PM
Views 9,135
;