how to count number of times file downloaded from my website using c# asp.net .I want to show it on website. please reply.thx in advance.

Dani AI

Generated

Quick summary: "downloaded" can mean two things — a user clicked a download link (a request) or the client actually received the entire file (a completed transfer). For a visible site counter you usually count requests that go through your app; if you must guarantee full-transfer counts you need post-processing of server logs or analytics.

A practical, low-risk pattern (works with 's idea) is:

  • Force downloads through a single server endpoint like /download?id=123.
  • Atomically record the event (INSERT to a log table or an UPDATE using SQL) and then hand the file off (redirect or let the server stream it). This counts requests reliably and lets you correct totals later from logs if needed.

Example SQL to atomically bump a counter:

UPDATE FileDownloads
SET DownloadCount = DownloadCount + 1
WHERE FileId = @FileId;

A minimal C# sketch to increment (use parameterized queries and connection pooling):

using(var cmd = new SqlCommand("UPDATE FileDownloads SET DownloadCount = DownloadCount+1 WHERE FileId=@id", conn)) {
  cmd.Parameters.Add("@id", SqlDbType.Int).Value = fileId;
  cmd.ExecuteNonQuery();
}

Notes and cautions:

  • Do not rely on Application state for totals — as suggested and warned, it resets on app-pool recycle and is not durable.
  • If files are served by a CDN or as static files, count via CDN logs or signed-URL redirects.
  • To measure truly completed transfers, parse IIS/W3C logs (match bytes sent) or use analytics/AWStats (as noted). For high traffic, write events to a queue/log table and aggregate offline to avoid contention.

Recommended Answers

All 10 Replies

With download code, you may update increment counter to a database or file.

Steps:
1. When user clicks on button to download a file.
2. Read increment counter value from database or from file.
3. Increment by 1.
4. Write to a file or update to a database.

but how am i going to know that file is downloaded? reply.

Well I presume that your files for downloading purpose are located in a folder named myfiles under web application root.

Write following code in Button's click event where file downloading process starts.

protected void Button1_Click(object sender, EventArgs e)
    {
       DownLoadFile("test.zip"); // This file is located under myfiles 
folder.
   }

Add the definition of DownLoadFile() function in your code section of your page.

void DownLoadFile(string filename)
    {
        string path = MapPath("~/files/" + filename);
        byte[] byte_array = System.IO.File.ReadAllBytes(path);

        Response.ClearContent();
        Response.ClearHeaders();

        Response.AddHeader("Content-Type", "application/octate-stream"); 
        Response.AddHeader("Content-Length", byte_array.Length.ToString()); 
        Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

        Response.BinaryWrite(byte_array);
        Response.Flush();
        Response.End();

        /****
        *  Write your code to update counter value - (Database or file)
        ****/
    }

>but how am i going to know that file is downloaded? reply.

You have to read the value of counter.

use application variable...

commented: I am not agree. -1

well why u didn;t agreed with application variable which is global...

or u suggest why not to use an application variable...?

Well I presume that your files for downloading purpose are located in a folder named myfiles under web application root.

Write following code in Button's click event where file downloading process starts.

protected void Button1_Click(object sender, EventArgs e)
    {
       DownLoadFile("test.zip"); // This file is located under myfiles 
folder.
   }

Add the definition of DownLoadFile() function in your code section of your page.

void DownLoadFile(string filename)
    {
        string path = MapPath("~/files/" + filename);
        byte[] byte_array = System.IO.File.ReadAllBytes(path);

        Response.ClearContent();
        Response.ClearHeaders();

        Response.AddHeader("Content-Type", "application/octate-stream"); 
        Response.AddHeader("Content-Length", byte_array.Length.ToString()); 
        Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

        Response.BinaryWrite(byte_array);
        Response.Flush();
        Response.End();

        /****
        *  Write your code to update counter value - (Database or file)
        ****/
    }

>but how am i going to know that file is downloaded? reply.

You have to read the value of counter.

The application variable is not persisted (saved). It will increment for the life of a session then go back to zero.

You're welcome.

Please mark this thread as solved if we have answered your question and good luck!

You can enable AWStats for your site.

This will geve you statistics about the pages viewd and other data like counts of file accessed etc.

You can enable this if your hosting supports this.

Regards,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.