c# - asp.net 中的文件处理程序

标签 c# asp.net httphandler ihttphandler

我需要跟踪 pdf 在我的网络应用程序中打开的时间。现在,当用户单击链接然后使用 window.open 从后面的代码中使用 window.open 时,我正在写入数据库,这并不理想,因为 Safari 会阻止弹出窗口,而其他网络浏览器会在运行时发出警告,所以我想Filehandler 是我需要使用的。我过去没有使用过 Filehandler,所以这行得通吗? pdf 不是二进制形式,它只是一个位于目录中的静态文件。

最佳答案

创建一个 ASHX(比 aspx onload 事件更快)页面,将文件的 ID 作为查询字符串传递以跟踪每次下载

 public class FileDownload : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
        {
            //Track your id
            string id = context.Request.QueryString["id"];
            //save into the database 
            string fileName = "YOUR-FILE.pdf";
            context.Response.Clear();
            context.Response.ContentType = "application/pdf";
            context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
            context.Response.TransmitFile(filePath + fileName);
            context.Response.End();
           //download the file
        }

在你的html中应该是这样的

<a href="/GetFile.ashx?id=7" target="_blank">

window.location = "GetFile.ashx?id=7";

但我更愿意坚持使用链接解决方案。

关于c# - asp.net 中的文件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19123961/

相关文章:

android - onSuccess(int, Header[], JSONObject) 未被覆盖,但收到回调

c# - 转换泛型和泛型类型

c# - ' Entity Framework 6.1。 3' already installed. Failed to add reference to ' System.ComponentModel.DataAnnotations'

c# - 无法将泛型类转换为泛型接口(interface),为什么?

c# - 如何使用javascript捕获鼠标点击时sharepoint页面上的每个元素?

javascript - 如何使用 RequireJS 优化处理内联代码

asp.net - 在 Visual Studio 构建中恢复 Bower 组件

c# - 缓存信息存储在 ASP.NET 中的什么位置?

azure - HTTP HEAD 响应中的 "303 See other"