I wanted to track how many times someone has downloaded a file that I have posted in my blog. Previously Mads posted an article about counting the number of times someone clicked a link. This post was met with some push back because of how it was simply redirecting the URL and could cause grief with SEO engines… A recommendation was made to create an extension that would handle this work during the event instead at the URL level.
So I started looking around and found two other people who have already done this extension:
I had a look at their extensions and decided they were both really cool but not exactly what I had in mind. I decided to write my own version. So what are the differences? Well I wrote my extension to record each file download into an XML file in the ~/app_data folder. I also wanted to record the last download date, the IP Address of the download, and how many times the file was downloaded. So here are the details of my version:
I record the download event into the file ~/app_data/filesdownloaded.xml (if it doesn't already exist) and record the following information:
- File name
- Last downloaded
- How many times it was downloaded
I thought about saving this into the Extension records, similar to my previous extension but decided it would probably be better keeping this information in it's own XML file. If you are in need of a different place to store this information, say a SQL database or in the Extension Manager data files, then please let me know and I will work on it for you…
To use this extension you just need to copy CountFileDownloads.cs into ~/app_code/extensions folder.
Now that you have implemented the tracking extension you can add a new Control that I created to display the results into a nice table:
Not sure if you can tell but I made the file name a link so that you can make this control available to anonymous users so they can see what others are downloading… Again, this can be changed either by me or by you…
To implement this control you just need copy FileDownLoadstats.cs into ~/app_code/Controls folder, then add <blog:FileDownloadStats ID="FileDownloadStats" runat="server" /> wherever you want the results displayed. For example, on my site where I use the Standard theme I added the following section:
<div class="box">
<h1>Files Downloaded</h1>
<blog:FileDownloadStats ID="FileDownloadStats" runat="server" />
</div>
That's it. Please let me know if you find this extension and control useful. Cheers!
UPDATE: I found a bug in the logic that allows anyone to enter in whatever text they want and it will be posted to my user control… So I changed the logic from:
FileHandler.Serving to FileHandler.Served
Double check the version you have and make sure it says 1.1. Sorry for the pain…




Kim Cameron's Identity Weblog
Mon, Feb 4, 2008
Technology