我们的客户希望能够在用户单击链接下载 PDF 时运行一些 javascript 跟踪代码。我的解决方案是将文件名作为查询字符串传递给页面,然后提示用户下载文件。无论如何让javascript在.net代码之前运行?
这是我在文件下载页面上使用的:
public partial class FileTracking : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string pdf = Request["pdf"] ?? string.Empty;
        if (string.IsNullOrEmpty(pdf)) return;
        Response.Clear();
        Response.AppendHeader("content-disposition", "attachment; filename=" + pdf);
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(pdf);
        Response.Flush();
        Response.End();
    }
}