ASP.Net Count Download Clicks

自作多情 提交于 2019-12-20 05:02:33

问题


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 the number of times, the users click on this link.

I thought in placing it in ajax update panel, to catch the postback and avoid full page refresh.

However, hyperlink doesn’t have a onClick method.

On the other hand I could use a linkbutton, which has a onClick built in. But It’s harder to make the file open in a new window… and I would also have to do something like:

Response.AppendHeader("Content-Disposition","attachment; filename=myImage.jpg");

But I heard that the above approach has some problems with PPT, PPTX, PPS, PPSX…

What is you'r opinion on this? How and why, would you do it?


回答1:


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>



回答2:


You can add an onclick to an asp hyperlink like so:

aHyperlinkControl.Attributes.Add("onclick", "jsCallToSendCountToServer();");


来源:https://stackoverflow.com/questions/2496583/asp-net-count-download-clicks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!