Download A File Using ASP MVC

You are looking to create some sort of HTML element on your site that when clicked will trigger a file download. Seems like a pretty simple request, but there is a little more to it then first appears. By default web browsers will have a set way to handle certain file formats. For example, if you wanted a user to be able to download an image file, simply putting the path of the image in the href, would just open the image in a new tab/window rather than actually download the file. Here is what you need to download a file using ASP MVC.

In order to do this you will need to setup an “ActionResult” on one of your controllers. This means you will be setting the href or source of the link to the URL of the controller. For this example i created a controller called “Services” a method in the controller called “DownloadFile” and this method accepted a value that represented the file. For this you could use some kind of ID that lets you know where to find the file or simple URL encode the file path. This method is probably the easiest, but is not secure. If you are using a public site, you are going to have to setup a DB table to manage the files so that each row has a unique ID that you can use to identify the file. This is what i have done for this example.

I have created a custom document object that works with the DB table. Passing the ID of the row in the constructor (i know its bad practice..but its much quicker). This will pull all of the file information from the database including the path of the file on the disk.

public class ServiceController : Controller
{
	public ActionResult DownloadFile(string id)
	{
		CustomDocumentObj document = new CustomDocumentObj(Int32.Parse(id));
		string filetype = Helpers.GetMimeType(document.FilePath);
		return File(document.FilePath, filetype, Path.GetFileName(document.FilePath));
	}
}

The file path returns a value like “C:/myfiles/somefile.pdf”.

In order to gain access to this file using a download link you will setup a link like the following.

<a href="/Services/DownloadFile/1">Download File</a>

And there you have it, this will trigger the file download in your browser. This can be used to download any file from a windows machine. So long as your server has access to the drive, the file can be downloaded. This means you can download files that are outside of the root of your web servers home directory.

Related Articles

Related Questions

Is the 2025 CKA Exam Really Harder?

I've been studying for the Certified Kubernetes Administrator (CKA) exam since September 2024, and I feel like I've put in a lot of effort....

Has Anyone Ever Experienced “Programmer’s Block”?

I've been coding on and off since the 90s and have a degree in it, but my day job isn't programming. Lately, I've been...

Is KodeCloud a Good Resource for Learning DevOps?

I've been exploring some DevOps tasks at work recently and I'm curious about whether KodeCloud is a reliable resource for diving into tools like...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Latest Tools

Online Hash Generator – String to Hash Converter

Need to quickly generate a hash from a string? Whether you're verifying file integrity, securing data, or just experimenting with cryptographic tools, this simple...

Convert CSV To HTML Table

Need to quickly turn CSV data into an HTML table? Whether you're copying data from Excel, Google Sheets, or another spreadsheet, this tool makes...

Student Group Randomizer

Creating fair and balanced groups in the classroom can be time-consuming — especially when you're trying to avoid repetition, manage different skill levels, or...

Random Group Generator

Need to split a list of people, items, or ideas into random groups? Our free Random Group Generator makes it quick and easy. Whether...

Flip Text Upside Down – Free Online Tool

Ever wanted to flip your text upside down just for fun or to grab someone’s attention in a creative way? This free online Upside...

Raffle Ticket Generator

If you're running a fundraiser, charity draw, or local event and need raffle tickets fast, this free online tool lets you generate and print...

Latest Posts

Latest Questions