I thought that this was easier…
I have a asp:hyperlink control, with target=”_blank”
, pointing to the file I want the user to download. My plan is to track
You might want to implement an IHttpHandler to track your downloads, as shown in this article for example.
Basically your handler's ProcessRequest() method would look something like this:
public void ProcessRequest(HttpContext context)
{
string file = context.Request.QueryString["file"];
// set content type and header (PDF in this example)
context.Response.ContentType = "application/pdf";
context.Response.AddHeader(
"Content-Disposition", "attachment; filename=" + file);
// assuming all downloadable files are in ~/data
context.Response.WriteFile(Server.MapPath("~/data/" + file));
context.Response.End();
}
Then your hyperlink to download a file would be like this:
<a href="/MyApp/MyHandler.ashx?file=someFile.pdf">...</a>
You can add an onclick
to an asp hyperlink like so:
aHyperlinkControl.Attributes.Add("onclick", "jsCallToSendCountToServer();");