Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Track File Download hits/count in ASP.Net

Tags:

c#

asp.net

Is there a way to inherently/manually log the number of times a specific file is accessed in an ASP site. For instance, I've got a few .mp3 files I have on my server and I would like to know how many times each file has been visited.

What's the best way to track this?

like image 525
Tyler Murry Avatar asked Jul 01 '10 03:07

Tyler Murry


2 Answers

Yes, There are several ways to do this. here's how you can do that.

Instead of serving mp3 file from the disk with a direct link like <a href="http://mysite.com/music/song.mp3"></a>, write an HttpHandler to serve the file download. In the HttpHandler, you can update the file-download-count in the database.

File download HttpHandler

//your http-handler
public class DownloadHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string fileName = context.Request.QueryString["filename"].ToString();
        string filePath = "path of the file on disk"; //you know where your files are
        FileInfo file = new System.IO.FileInfo(filePath);
        if (file.Exists)
        {
            try
            {
                //increment this file download count into database here.
            }
            catch (Exception)
            {
                //handle the situation gracefully.
            }
            //return the file
            context.Response.Clear();
            context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
            context.Response.AddHeader("Content-Length", file.Length.ToString());
            context.Response.ContentType = "application/octet-stream";
            context.Response.WriteFile(file.FullName);
            context.ApplicationInstance.CompleteRequest();
            context.Response.End();
        }
    }
    public bool IsReusable
    {
        get { return true; }
    }
}  

Web.config configuration

//httphandle configuration in your web.config
<httpHandlers>
    <add verb="GET" path="FileDownload.ashx" type="DownloadHandler"/>
</httpHandlers>  

Linking file download from front-end

//in your front-end website pages, html,aspx,php whatever.
<a href="FileDownload.ashx?filename=song.mp3">Download Song3.mp3</a>

Additionally, you can map the mp3extension in web.config to an HttpHandler. To do this,you'll have to make sure,you configure your IIS to forward .mp3 extension requests to asp.net worker process and not serve directly and also make sure the mp3 file is not on the same location that the handler capturing, if the file is found on disk on the same location then HttpHandler will be over-ridden and the file will be served from the disk.

<httpHandlers>
    <add verb="GET" path="*.mp3" type="DownloadHandler"/>
</httpHandlers>
like image 103
this. __curious_geek Avatar answered Nov 04 '22 08:11

this. __curious_geek


What you can do is that you create an Generic Handler (*.ashx file) and then access the file through:

Download.ashx?File=somefile.mp3

In the handler you can run code, log the access and return the file to the browser.
Make sure you do the right security checks as this can potentialy be used to access any file in your web directory or even on the whole file system!

If you know all your files are *.mp3, a second option is to add this into the httpHandlers section of your web.config file:

<add verb="GET" path="*.mp3" type="<reference to your Assembly/HttpHandlerType>" />

And run code in your HttpHandler.

like image 2
Xavier Poinas Avatar answered Nov 04 '22 10:11

Xavier Poinas