Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
67 lines (52 sloc) 2.44 KB
author comments date layout slug title desc wordpress_id categories tags
jokecamp
true
2012-07-26 13:06:46 +0000
post
how-to-create-an-asp-net-httphandler-to-serve-files-from-local-system
How to create an ASP.NET HttpHandler to serve files from local system
Tutorial on how to create an ASP.NET HttpHandler to serve files from local system with C# code
340
Code
asp.net
audio
C#
html5

**How do you serve files on a local system (or network share) from an ASP.NET website? **

The problem is that browsers do not play well with file:/// as links. IE will allow it but Chrome and Firefox will block these links for security purposes. See http://stackoverflow.com/questions/1254572/cross-browser-link-to-file-on-local-system

One solution is to create an HTTP Handler in your ASP.NET application. This handler will simply retrieve the network file and serve it up.

public class Downloader : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string path = "//networkshare/path/hey.wav";

        // Hard coded but you can easily get from url parameters
        // string path = context.Request["path"];

        var file = new FileInfo(path);

        // You should check for file.Exists !

        context.Response.Clear();
        context.Response.AddHeader("content-disposition",
            string.Format("attachment; filename=\"{0}\"", file.Name));
        context.Response.ContentType = "audio/wav";
        context.Response.WriteFile(file.FullName, false);
    }

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

Now we can allow users to download the file with direct link Downloader.ashx?path=x.

Audio Files

If you are serving audio files (as my example is doing) you can serve the file with the new HTML5 audio tag. Be sure your Response.ContentType agrees with the audio source type. Support for the different mime types and codecs is very inconsistent. You will want to earn more about the current state of HTML 5 audio and modern browsers before getting too deep in audio.

<audio controls="controls">
  <source src="Downloader.ashx?path=x" type="audio/wav" />
  Your browser does not support the audio element.
</audio>

See if your browser supports the new HTML 5 audio tags