【问题标题】:Filehandler in asp.netasp.net 中的文件处理程序
【发布时间】:2013-10-08 01:43:29
【问题描述】:

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

【问题讨论】:

  • @patxy IHttpHandler 实现可以并且已经用于此类事情,这是肯定的。
  • 我只是在寻找一种写入数据库的方法,然后在另一个选项卡中打开文件而不必使用 window.open
  • @SamCromer 如果您不想制作自己的 HttpHandler,那么只需将链接更改为链接按钮,在回发时执行您需要的代码,并将用户重定向到文档。否则,考虑reading this
  • @MikeSmithDev ,这就是我目前正在做的事情,但是弹出窗口阻止程序正在破坏聚会,野生动物园甚至没有发出警告,它只是不起作用。这是我所做的: protected void AccessFile_Click(object sender, EventArgs e) { 在所有版本的 Safari string newWindowUrl = "pdf/Newsletter01.pdf#zoom=100"; string javaScript = "\n"; this.RegisterStartupScript("", javaScript); }
  • HTML - ​​ton>

标签: c# asp.net httphandler ihttphandler


【解决方案1】:

创建一个 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";

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

【讨论】:

  • 谢谢,这就是我所得到的,我以前没有使用过 FileHandler,我想知道它是否可以解决这个问题。谢谢!
  • 是的,这个问题已经存在一段时间了,ashx 会很容易地解决你的问题。
【解决方案2】:

这是自定义 HttpHandler 的一个选项,它使用常规锚标记到 PDF:

创建 ASHX(右键单击您的项目 -> 添加新项目 -> 通用处理程序)

using System.IO;
using System.Web;

namespace YourAppName
{
    public class ServePDF : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string fileToServe = context.Request.Path;
            //Log the user and the file served to the DB
            FileInfo pdf = new FileInfo(context.Server.MapPath(fileToServe));
            context.Response.ClearContent();
            context.Response.ContentType = "application/pdf";
            context.Response.AddHeader("Content-Disposition", "attachment; filename=" + pdf.Name);
            context.Response.AddHeader("Content-Length", pdf.Length.ToString());
            context.Response.TransmitFile(pdf.FullName);
            context.Response.Flush();
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

编辑 web.config 以将您的处理程序用于所有 PDF:

<httpHandlers>
    <add verb="*" path="*.pdf" type="YourAppName.ServePDF" />
</httpHandlers>

现在,PDF 的常规链接将使用您的处理程序来记录活动并提供文件

<a href="/pdf/Newsletter01.pdf">Download This</a>

【讨论】:

  • 是的,我考虑过 Response.Redirect,但我不希望他们离开网站。
  • 好吧,如果不能打开一个新窗口,并且不能将它们保持在同一个选项卡/页面中......你打算做什么?
  • 我想我在之前的文章中提到过我需要新标签页 Response.Redirect 在同一个标​​签页/窗口中打开,这不是一个选项。 "无需使用 window.open 在另一个选项卡中打开文件"
  • @SamCromer 这不是很清楚。我知道您已经接受了另一个答案,但我认为这是一个更容易使用的选项,因为您不会弄乱 ID 等。这里是社区。​​span>
  • 您可以考虑使用 context.ApplicationInstance.CompleteRequest 而不是 context.Request.End。有关更多信息,请参阅这些链接。 stackoverflow.com/questions/7867238/alternative-to-response-end,关于结束请求的建议 - blogs.msdn.com/b/aspnetue/archive/2010/05/25/…,以及如何在不发送不需要的数据的情况下使用 CompleteRequest - stackoverflow.com/questions/1087777/…
猜你喜欢
  • 2012-09-28
  • 2011-08-09
  • 2010-10-11
  • 1970-01-01
  • 1970-01-01
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
  • 2011-06-14
相关资源
最近更新 更多