ASP.Net Count Download Clicks

前端 未结 2 1104
眼角桃花
眼角桃花 2021-01-24 08:23

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

相关标签:
2条回答
  • 2021-01-24 09:10

    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>
    
    0 讨论(0)
  • 2021-01-24 09:24

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

    aHyperlinkControl.Attributes.Add("onclick", "jsCallToSendCountToServer();");
    
    0 讨论(0)
提交回复
热议问题