Download tracking file / count in ASP.Net

Is there a way to essentially / manually register the number of hits on a particular file on an ASP site. For example, I have several .mp3 files that I have on my server, and I would like to know how many times each file has been visited.

What is the best way to track this?

+6
source share
3 answers

Yes There are several ways to do this. here is how you can do it.

Instead of submitting an mp3 file from a disc with a direct link, for example <a href="http://mysite.com/music/song.mp3"></a> , write HttpHandler to download the file. In HttpHandler you can update the number of download files in the database.

Download HttpHandler File

 //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> 

Link file upload from frontend

 //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 mp3 extension in web.config to HttpHandler. To do this, you will need to make sure that you configure IIS to redirect .mp3 extension requests to the asp.net workflow and do not serve directly, and also make sure that the mp3 file is not in the same place as the capture handler if the file is located on the disk in the same place, the HttpHandler will be overloaded, and the file will be sent from the disk.

 <httpHandlers> <add verb="GET" path="*.mp3" type="DownloadHandler"/> </httpHandlers> 
+12
source share

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

Download.ashx? File = somefile.mp3

In the handler, you can run the code, register access and return the file to the browser.
Make sure you do the right security checks, as it can potentially be used to access any file in your web directory or even the entire file system!

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

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

And run the code in HttpHandler.

+2
source share

The problem with using HttpHandler to count the load is that it works for a very long time when someone starts downloading your file. But many internet spiders, search engines, etc. Just starting the download, and cancel it very soon! And you will notice how they downloaded the file.

It is best to make an application that parsed your IIS statistics file. This way you can check how many bytes are loaded by the user. If the bytes are the same or larger than the size of your files, this means that the user has uploaded the complete file. Other attempts were just attempts.

+1
source share

All Articles