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

Why isn’t PSWindowsUpdate detecting all updates?

Hey everyone! I'm having trouble with PSWindowsUpdate not recognizing all the available updates, specifically the recent KB5060829. I want to hide this update, but...

Is This PC Build Good for Productivity and Gaming at $1300?

I'm putting together my first PC and I'm trying to figure out if my build is solid enough for productivity tasks and some casual...

Is This Gaming PC Build a Good Xbox One Replacement?

I recently had my Xbox One die after about 10 years of use, and I'm thinking of switching to a compact gaming PC. My...

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