Want to let user to download a HTML file or a GIF file?
Here is an example that was used on High Country website to let user download MP3 files.
Create a Generic Handler, let's call it Download.ashx
<%@ WebHandler Language="C#" Class="Download" %>
using System;
using System.Web;
public class Download : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string fname = context.Request.QueryString["audio"]; // This would be the filename without extension
if (fname == null) // No query string
{
context.Response.ContentType = "text/plain";
context.Response.Write("Error");
}
else
{
fname = "audio/" + fname + ".mp3"; // Complete the filename/path
string filepath = context.Server.MapPath(fname);
if (!System.IO.File.Exists(filepath)) // File not found
{
context.Response.ContentType = "text/plain";
context.Response.Write("404 File Not Found");
}
else // Let user download the file
{
context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + System.IO.Path.GetFileName(fname) + "\"");
context.Response.ContentType = "audio/mpeg";
context.Response.WriteFile(filepath);
}
}
}
public bool IsReusable { get { return false; } }
}
Then in the page that contains the link to the mp3, put this link
<a href="Download.ashx?audio=BrightToOmeo/01 Introduction">4.9 MB DOWNLOAD</a>