asp:Gridview中的超链接在新的浏览器窗口中打开PDF [英] asp:Hyperlink in Gridview open PDF in new browser window
问题描述
我目前有一个gridview可以动态创建PDF文件的asp:Hyperlinks
.现在,URL只是将文件下载到本地计算机.使它们在新窗口中打开以便用户可以选择打印或下载的最简单方法是什么. Chrome会自动下载它. IE要求打开或保存它.我只希望它在浏览器窗口中打开.
I currently have a gridview that dynamically creates asp:Hyperlinks
to PDF files. Right now, the url just downloads the file to the local machine. What's the easiest way to get them to open in a new window so the user has the option to print or download. Chrome automatically downloads it. IE asks to open or save it. I just want it to open in a browser window.
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink ID="lblProductName" runat="server" Text='<%# Eval("name") %>' NavigateUrl="#" Target="_blank" ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
然后在后面的代码中创建实际的NavigateUrl
And the actual NavigateUrl gets created in the code behind
HyperLink lblProductName = (HyperLink)e.Row.FindControl("lblProductName");
lblProductName.NavigateUrl = urlLink;
推荐答案
添加一个新页面,例如DownloadFile.aspx并添加以下代码
Add a new page say DownloadFile.aspx and add the following code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string fileName = Request.QueryString["pdffile"];
string path = Server.MapPath("~/PDFs/") + fileName;
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.WriteFile(path);
Response.Flush();
Response.End();
}
}
现在在GridView中删除LinkButton并添加HyperLink,如下所示
Now in GridView remove LinkButton and add HyperLink as shown below
<asp:HyperLink ID="HyperLink1" runat="server" Target = "_Blank" NavigateUrl='<%# Eval("FileName","DownloadFile.aspx?PDFFile={0}") %>'></asp:HyperLink>
这篇关于asp:Gridview中的超链接在新的浏览器窗口中打开PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!