开发者

Track File Download hits/count in 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?


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>


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.


Problem with using HttpHandler for download counting is that it will fire very time somebody starts downloading your file. But many Internet spiders, search engines, etc will just start download, and very soon cancel it! And you will be noticed as they downloaded file.

Better way is to make an application that analyzed your IIS stats file. So you can check how many bytes user downloaded. If bytes are same or bigger then your files size, it means user downloaded complete file. Other tries were just attempts.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜