Tuesday, 25 September 2012

ASP: how to keep track of downloaded files


Today we are going to see how we can keep track of downloaded files in a web site.
We normally link files with an hyperlink. The trick in this case is to pass the file information to another ASP page and then - after "taking note" of the relevant information - initiate the download.
Ready to see the code? Follow me...
The code
Ok folks, this time things are very easy.
We need to create hyperlinks to files like:
<a href="dwnchk.asp?filename=test.zip">Download test.zip</a>
The above code will simply redirect the user to another page (dwnchk.asp). At the same time we are passing the information needed to complete our "evil" plan: keep track of what is downloaded!

When the dwnchk.asp page is loaded we just need to get the filename, pass it to a DB or whatever we prefer. For example we can increase a counter on a db: if the file name is already there, increase a counter by one, if not create a new record and set the counter to one.
All the above is easily done... if you read The web thought you can find all you need in order to create suitable queries.
When the db update is done, we can initiate the download with something like:
Response.ContentType = "application/x-zip-compressed"
Response.Redirect request("filename")
Easy, isn't it?

This is really a simple solution as you can see... Just one more thing! Remember: don't write any HTML code in dwnchk.asp.

0 thoughts:

Post a Comment

Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.