Beginner's question - Catch a "HyperLink" click event

L

Larry Smith

Hi there,

I'm fairly new to ASP.NET and web development but an experienced developer
otherwise (C++, C#, etc.). Can someone point me in the right direction on
how to count the number of times a given file has been downloaded. I've set
this up using a "HyperLink" control where the "NavigateUrl" property points
to the file on my site . I simply want to track the number of times each
file has been download. How do I pull this off and can someone recommed how
to track statistics in general (what does ASP.NET and/or IIS provide
out-of-the-box for instance). Thanks in advance.
 
E

Eliyahu Goldin

If the link points straight to the file, no one will take any note on the
download. I would instead make a page GetFile.aspx that will except the file
name (or some sort of id for the file), stream down the file to the client
browser and increment a counter that you will need to setup in some
persistant location, e.g. database. You will need to set the NavigateUrl
property to something like GetFile.asp?file=myfile
 
B

bruce barker

the iis logs provide all this information in a data file or sqlserver
table. but supplies no reporting tools. google iis log analysis and pick
one.

-- bruce (sqlwork.com)
 
E

Eliyahu Goldin

Streamimg down files is simple. Response.TransmiteFile(fileName) will do it
but you may need to set some HTTP headers ro ensure correct content
disposition etc. It could be something like this:
FileInfo fileToSend = {get your file info}
this.Response.AddHeader("Content-Disposition", String.Format("attachment;
filename={0}", fileToSend.Name));
this.Response.AddHeader("Content-Length", fileToSend.Length.ToString());
this.Response.ContentType = "text/plain";

Modify it for your case.
 
L

Larry Smith

the iis logs provide all this information in a data file or sqlserver
table. but supplies no reporting tools. google iis log analysis and pick
one.

Thanks. I'll investigate this further. Appreciated.
 
L

Larry Smith

Streamimg down files is simple. Response.TransmiteFile(fileName) will do
it but you may need to set some HTTP headers ro ensure correct content
disposition etc. It could be something like this:
FileInfo fileToSend = {get your file info}
this.Response.AddHeader("Content-Disposition", String.Format("attachment;
filename={0}", fileToSend.Name));
this.Response.AddHeader("Content-Length", fileToSend.Length.ToString());
this.Response.ContentType = "text/plain";

Modify it for your case.

Thanks again. This is very useful to know and I'll study it further. Note
that I also discovered "Response.Redirect()" so presumably I can rely on
this as well. Based on your last post, I can therefore pass a variable from
multiple links (each identifying a particular file), invoke
"Request.QueryString()" in "Page_Load()", and then redirect the user to the
requested file (and update my download counter of course). I've already
successfully tested this. Note BTW that one useful thing I did discover
about this method is that you can apparently hide the location of the actual
downloaded file. By using a link as you suggested, say
"GetFile.aspx?file=whatever", the location of "whatever" is now determined
on the server side so the client doesn't even see it's (virtual) path. This
improves the security of the site since the user doesn't know where things
are stored. I'm not sure if this is actually the case however but I can
always copy the file to the root folder or some other harmless location
before redirecting. Thanks again for your help. Greatly appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top