How To Download a File Using ASHX Handler

File downloads can be a bit of a nuance sometimes as web browsers often support a lot of document types. Simply adding the file path to the href of a html element, wont be enough for you to be able to download the file. There are dozens of work arounds to make this work, but using an ASP.NET ashx handler is definitely one of the cleanest methods to do it using code.

I will paste the contents of the entire ashx file below as the script is very simple. This solution will only work if you are using the non razor MVC models that are part of the ASP.NET framework. First you will need to create a new “Generic Handler” this will create a file with a .ashx extension. Once this is done you can point all of your links to this page when you want to download a file. There are many ways you can direct the downloads to this page. To keep things very simple, I have setup the script to accept the file path in the URL of the request.

In order to make the request work you will need to urlencode the value that is being passed into the url. The following code will be all you need to setup the url.

<% filename = "//somepath/folder/myfile.text"; %>
<a href="scripts/Downloadfile.ashx?file=<%= HttpUtility.UrlEncode(filename) %>">Download</a>

You could alternatively use javascript or pretty much anything that is capable of generating a request to your download script. This method has massive security risks, so please take this into consideration. This code will be enough to get a working prototype for you to built upon. This should never be used in any public facing code without some security measures being implemented to stop people tampering with the URL.

namespace scripts
{
    public class Downloadfile : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlDecode(context.Request["file"]));

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

Related Articles

Related Questions

How can I work with Excel files that include formulas in programming?

I have a situation where a client provides an Excel file containing their own formulas. I need to input some variables into it and...

Why Can’t I Shrink My SSD Partition More Than 300MB?

I'm trying to shrink my 500GB SSD, which currently has about 200GB of free space. However, whenever I attempt to shrink the partition, it...

How to Efficiently Merge XML Files from Multiple Devices?

I have a system with over 31,000 devices sending data every 5 minutes through a REST API. Each request triggers a Lambda function that...

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