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.

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.