954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to count number of times file downloaded from my website

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.

manoj_dabs
Newbie Poster
3 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

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.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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

manoj_dabs
Newbie Poster
3 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

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.

__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

use application variable...

dnanetwork
Practically a Master Poster
Banned
633 posts since May 2008
Reputation Points: 28
Solved Threads: 106
 

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.

dnanetwork
Practically a Master Poster
Banned
633 posts since May 2008
Reputation Points: 28
Solved Threads: 106
 

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

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 
dnanetwork
Practically a Master Poster
Banned
633 posts since May 2008
Reputation Points: 28
Solved Threads: 106
 

You're welcome.

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

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

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,

johnly
Newbie Poster
17 posts since Nov 2008
Reputation Points: 10
Solved Threads: 4
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You