跟踪文件下载ASP.Net中的命中/计数

时间:2022-04-09 15:19:32

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.

有没有办法固有/手动记录ASP站点中访问特定文件的次数。例如,我在服务器上有一些.mp3文件,我想知道每个文件访问过多少次。

What's the best way to track this?

追踪这个的最佳方法是什么?

3 个解决方案

#1


12  

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.

不使用 等直接链接从磁盘上提供mp3文件,而是编写一个HttpHandler来提供文件下载。在HttpHandler中,您可以更新数据库中的file-download-count。

File download HttpHandler

文件下载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.

此外,您可以将web.config中的mp3extension映射到HttpHandler。要做到这一点,你必须确保,你配置你的IIS将.mp3扩展请求转发到asp.net工作进程而不是直接提供,并确保mp3文件不在处理程序捕获的同一位置,如果在同一位置的磁盘上找到该文件,则HttpHandler将被覆盖,并且该文件将从磁盘提供。

<httpHandlers>
    <add verb="GET" path="*.mp3" type="DownloadHandler"/>
</httpHandlers>

#2


2  

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

你可以做的是你创建一个通用处理程序(* .ashx文件),然后通过以下方式访问该文件:

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!

在处理程序中,您可以运行代码,记录访问权限并将文件返回给浏览器。确保您进行了正确的安全检查,因为这可能会被用于访问您的Web目录中的任何文件,甚至是整个文件系统!

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

如果您知道所有文件都是* .mp3,则第二个选项是将其添加到web.config文件的httpHandlers部分:

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

And run code in your HttpHandler.

并在您的HttpHandler中运行代码。

#3


1  

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.

使用HttpHandler进行下载计数的问题在于,当有人开始下载文件时,它会激活。但是很多互联网蜘蛛,搜索引擎等都会开始下载,很快就会取消它!当他们下载文件时你会被注意到。

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.

更好的方法是创建一个分析IIS统计信息文件的应用程序。因此,您可以检查用户下载的字节数。如果字节与文件大小相同或更大,则表示用户下载了完整文件。其他尝试只是尝试。

#1


12  

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.

不使用 等直接链接从磁盘上提供mp3文件,而是编写一个HttpHandler来提供文件下载。在HttpHandler中,您可以更新数据库中的file-download-count。

File download HttpHandler

文件下载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.

此外,您可以将web.config中的mp3extension映射到HttpHandler。要做到这一点,你必须确保,你配置你的IIS将.mp3扩展请求转发到asp.net工作进程而不是直接提供,并确保mp3文件不在处理程序捕获的同一位置,如果在同一位置的磁盘上找到该文件,则HttpHandler将被覆盖,并且该文件将从磁盘提供。

<httpHandlers>
    <add verb="GET" path="*.mp3" type="DownloadHandler"/>
</httpHandlers>

#2


2  

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

你可以做的是你创建一个通用处理程序(* .ashx文件),然后通过以下方式访问该文件:

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!

在处理程序中,您可以运行代码,记录访问权限并将文件返回给浏览器。确保您进行了正确的安全检查,因为这可能会被用于访问您的Web目录中的任何文件,甚至是整个文件系统!

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

如果您知道所有文件都是* .mp3,则第二个选项是将其添加到web.config文件的httpHandlers部分:

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

And run code in your HttpHandler.

并在您的HttpHandler中运行代码。

#3


1  

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.

使用HttpHandler进行下载计数的问题在于,当有人开始下载文件时,它会激活。但是很多互联网蜘蛛,搜索引擎等都会开始下载,很快就会取消它!当他们下载文件时你会被注意到。

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.

更好的方法是创建一个分析IIS统计信息文件的应用程序。因此,您可以检查用户下载的字节数。如果字节与文件大小相同或更大,则表示用户下载了完整文件。其他尝试只是尝试。