我需要跟踪 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/