如何使用ASP.NET跟踪下载量?如何使用ASP.NET跟踪下载量?
我想找到有多少用户完成文件下载?
另外如何限制用户从特定的IP?
例如,如果用户下载http://example.com/file.exe
,该轨道将自动工作。
如何使用ASP.NET跟踪下载量?如何使用ASP.NET跟踪下载量?
我想找到有多少用户完成文件下载?
另外如何限制用户从特定的IP?
例如,如果用户下载http://example.com/file.exe
,该轨道将自动工作。
如果你想从您的网站下载计,创建一个下载页面,计数请求:
链接文件应该像Download.aspx?file=123
protected void Page_Load(object sender, EventArgs e)
{
int id;
if (Int32.TryParse(Request.QueryString["file"], out id))
{
Count(id); // increment the counter
GetFile(id); // go to db or xml file to determine which file return to user
}
}
或者Download.aspx?file=/files/file1.exe
:
protected void Page_Load(object sender, EventArgs e)
{
FileInfo info = new FileInfo(Server.MapPath(Request.QueryString["file"]));
if (info.Exists)
{
Count(info.Name);
GetFile(info.FullName);
}
}
限制访问您的下载页面:
protected void Page_Init(object sender, EventArgs e)
{
string ip = this.Request.UserHostAddress;
if (ip != 127.0.0.1)
{
context.Response.StatusCode = 403; // forbidden
}
}
我想跟踪直接链接,而不是重定向的项目。 – pedram 2010-08-02 10:35:16
@pedram:跟踪本地服务器的下载? – abatishchev 2010-08-02 10:53:27
使用HttpHandler作为下载部分。例如,您可以使用OutputStream。当这个处理程序被调用时,你可以更新数据库中的一个计数器。
另外如何限制用户从特定的IP?
有几种方法可以做到这一点。这里是你如何做到这一点。
不要使用像<a href="http://mysite.com/music/file.exe"></a>
这样的直接链接从磁盘上提供文件,请编写HttpHandler
来提供文件下载。在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配置
//httphandle configuration in your web.config
<httpHandlers>
<add verb="GET" path="FileDownload.ashx" type="DownloadHandler"/>
</httpHandlers>
从前端链接文件下载
//in your front-end website pages, html,aspx,php whatever.
<a href="FileDownload.ashx?filename=file.exe">Download file.exe</a>
此外,您可以将web.config中的exe
扩展名映射到HttpHandler。要做到这一点,您必须确保,您将IIS配置为将.exe扩展请求转发给asp.net工作进程,而不是直接提供服务,并确保mp3文件与处理程序捕获的位置不同,如果在同一位置的磁盘上找到该文件,则HttpHandler将被覆盖并且该文件将从磁盘提供。
<httpHandlers>
<add verb="GET" path="*.exe" type="DownloadHandler"/>
</httpHandlers>
谢谢, 但我真的需要直连吗? 我无法真正使用FileDownload.ashx?filename = file.exe。 – pedram 2010-08-02 10:42:18
将exe扩展映射到asp.net服务。请阅读文章的最后部分。 – 2010-08-02 11:09:14
可能的重复的[跟踪文件下载命中/计数在ASP.Net](http:// stackoverflow。com/questions/3154856/track-file-download-hits-count-in-asp-net) – 2010-08-02 10:33:26