Published on: 21 February, 2016

How to trigger a download file in c# MVC controller

Posted by andy | 1794 views 0 likes 0 favourites 2 comments
Tumblr Digg Google Plus

you want to know how you can easily trigger a popup download in MVC controller rather than using a href link to open into a new window?

Below is the simple script on how you can do it.

public FileContentResult Download(long listingID) {
	var document = new FileInfo(Server.MapPath("~/sample.pdf"));
	if (document != null) {
		Response.AppendHeader("Content-Disposition", "FileName=sample.pdf");
		return File(System.IO.File.ReadAllBytes(filepath), "application/octet-stream");
	return null;
John A Davis
Commented on: 03 Apr, 2017 11:45:43 AM
How do I call FileContentResult. I tried like this but got an error (I took out the (long listingID) and used a hardcoded path to a .jpg): @Html.ActionLink("Download your pictures", "FileContentResult", "Home"); I got this error: The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /Home/FileContentResult
Commented on: 03 Apr, 2017 03:02:01 PM
Could you confirm if the path is correct? If you want to use FileContentResult, you can use something like this. byte[] content = your_byte[]; FileContentResult result = new FileContentResult(content, "application/octet-stream") { FileDownloadName = "file_name"; }; return result;
Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.
Share your article with us and add your own google adsense account to earn extra money. Plus, you can promote a link back to your site.
Related Articles
Published on: 21 March, 2018
In this tutorial you will learn how to create your own custom identity authentication and authorization with ASP.Net MVC without using Entity Framework. By default, the example given in the MVC official tutorial site is using Entity Framework. So if you do not want to use Entity Framework and want to use external data source or your own database, you may want to read the following article I wrote on how to integrate your existing login details.
Published on: 01 March, 2018

If you want to enable routing in C# MVC, you have to do the following steps. Note: this only applies to MVC version 5 or above. Open your RouteConfig.cs file under App_Start folder in your MVC root project.

Published on: 27 October, 2017
In this article, you will learn how to download a file in Web API MVC. We will use HTTP Get API method to perform this download action with one single parameter which will accept the file name.
Published on: 28 October, 2016

In this tutorial you will learn how easily you can implement a simple checkout express using C# MVC Web API. We will create a really simple shopping cart where customers can add and delete their cart items before proceed to payment.