views:

44

answers:

3

How to track downloads with ASP.NET?

I want to find how many users completed the file download?

Also How can make restrict user from specific IP?

for example if a user download http://example.com/file.exe, the track will work automatically.

+2  A: 

If you want to count downloads from your site, create a Download page and count requests:

Links to files should look like 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
     }
}

Or 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);
     }
}

To restrict access to your Download page:

protected void Page_Init(object sender, EventArgs e)
{
     string ip = this.Request.UserHostAddress;
     if (ip != 127.0.0.1)
     {
         context.Response.StatusCode = 403; // forbidden
     }
}
abatishchev
I want to track Direct Links , not redirected items .
pedram
@pedram: Track downloads from local server?
abatishchev
A: 

Use a HttpHandler for the downloading part. You can for example use the OutputStream. When this handler gets called you can update a counter in the database.

Also How can make restrict user from specific IP ?

For this you can use a HttpModule: take a look at these samples.

XIII
+1  A: 

There are several ways to do this. here's how you can do that.

Instead of serving file from the disk with a direct link like <a href="http://mysite.com/music/file.exe"&gt;&lt;/a&gt;, 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=file.exe">Download file.exe</a>

Additionally, you can map the exeextension in web.config to an HttpHandler. To do this,you'll have to make sure,you configure your IIS to forward .exe 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="*.exe" type="DownloadHandler"/>
</httpHandlers>
this. __curious_geek
thank you ,But I need Direct Link really ?I can not use FileDownload.ashx?filename=file.exe really .
pedram
map the exe extension to serve with asp.net. please read the last part of the article.
this. __curious_geek