-
May 12th, 2011, 07:44 PM
#1
File Download Counter
I try to implement the code from this link to count the files downloaded from my site
http://blog.donnfelker.com/2008/04/2...r-httphandler/
It works fine only for a single file. The files that needs to be downloaded is defined by this string
private readonly string MY_FILE = "App_Data/Example.pdf";
The problem I have, I have the following files to download at users choice.
program.exe
manual.pdf
I want to know how can I modify it to enable it to work with different files users might select. I try to modify the httprequest, I mean inside this function SendFileToUser to determine which file users might select and I have not been able to do it right
the original code
PHP Code:
using System;
using System.IO;
using System.Text;
using System.Web;
using System.Xml;
using System.Web.UI;
/// <summary>
/// Increments a counter and forces a download to the user.
/// </summary>
public class DownloadHandler : IHttpHandler
{
private readonly string COUNTER_REPOSITORY = "App_Data/ExampleCounter.xml";
private readonly string MY_FILE = "App_Data/Example.pdf";
private readonly string SAMPLE_DOWNLOAD_COUNT_ELEMENT = "SampleDownloadCount";
#region IHttpHandler Members
///<summary>
///Enables processing of HTTP Web requests
/// by a custom HttpHandler that implements
/// the <see cref="T:System.Web.IHttpHandler"></see> interface.
///</summary>
///<param name="context">An
/// <see cref="T:System.Web.HttpContext"></see> object that provides
/// references to the intrinsic server objects (for example, Request,
/// Response, Session, and Server) used to service HTTP requests. </param>
public void ProcessRequest(HttpContext context)
{
IncrementCounter(context);
SendFileToUser(context);
}
///<summary>
///Gets a value indicating whether another request can use
/// the <see cref="T:System.Web.IHttpHandler"></see> instance.
///</summary>
///
///<returns>
///true if the <see cref="T:System.Web.IHttpHandler"></see> instance
/// is reusable; otherwise, false.
///</returns>
///
public bool IsReusable
{
get { return false; }
}
#endregion
/// <summary>
/// Sends the file to the user.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> that
/// contains the request.</param>
private void SendFileToUser(HttpContext context)
{
// Send the file to the user.
FileInfo fileInfo = new FileInfo(context.Server.MapPath(MY_FILE));
context.Response.ContentType = "application/pdf";
context.Response.AddHeader(
"Content-Disposition",
"attachment; filename=" + fileInfo.Name);
context.Response.WriteFile(fileInfo.FullName);
context.Response.End();
}
/// <summary>
/// Increments the counter.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> that contains t
/// he current request</param>
private void IncrementCounter(HttpContext context)
{
int value = int.MinValue;
string fileName = context.Server.MapPath(COUNTER_REPOSITORY);
using (XmlTextReader reader = new XmlTextReader(fileName))
{
reader.ReadStartElement(SAMPLE_DOWNLOAD_COUNT_ELEMENT);
value = Convert.ToInt32(reader.ReadString());
}
value++;
using (XmlTextWriter writer = new XmlTextWriter(fileName, Encoding.ASCII))
{
writer.WriteElementString(
SAMPLE_DOWNLOAD_COUNT_ELEMENT,
value.ToString());
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|