【问题标题】:Open pdf file in browser with url generated in asp.net使用asp.net中生成的url在浏览器中打开pdf文件
【发布时间】:2011-03-03 13:04:51
【问题描述】:

我在 asp.net 中使用 .net 4.0。

我在 web 服务器上有一些 pdf 文件的文件夹,我在 gridview 中显示它的(文件名)名称然后我想做“当我在网格视图中单击项目然后它打开该 pdf 文件并生成带有文件的 url浏览器。”

我正在使用以下代码

GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).Parent.Parent;
LinkButton hk = (LinkButton)gvFiles.Rows[row.RowIndex].FindControl("lnkbtnTitleView");
string s = Server.MapPath("~/AppName/App_" + dtFiles.Rows[0]["ENewsLetterID"].ToString() + "_1.PDF");
hk.Attributes.Add("onclick","window.open('"+s+"')");

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    将 ashx 处理程序添加到您的网站,这会将 pdf 作为内容发送回客户端。添加指向 gridview 的链接,该链接将引用此处理程序。

    【讨论】:

      【解决方案2】:

      如果 PDF 文件已经存在,只需输入直接链接:

      string s = string.Format("/AppName/App_{0}_1.PDF", dtFiles.Rows[0]["ENewsLetterID"]);
      

      并添加窗口名称_blank:

      hk.Attributes.Add("onclick", string.Format("window.open('{0}', '_blank');", s));
      

      否则请提供更多细节:PDF应该如何生成?

      【讨论】:

      • 其工作但新的浏览器窗口会自动打开和关闭。不要在浏览器窗口中打开 pdf 文件,而是直接在 Acrobat 阅读器中打开。
      • @sur 这取决于浏览器...如果安装了正确的插件/扩展,PDF 将直接显示在新的浏览器窗口中。要强制下载文件,请参阅 gor answer,您必须添加正确的标题 Content-type: application/pdf、Content-Disposition: attachment; filename=[name here] 并使用类似 Response.WriteFile
      • 当我写它开始下载文件。我只想在带有 url 的新浏览器窗口中打开文件。
      猜你喜欢
      • 2016-05-04
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多