[英]How To Open PDF Files In New Tab In Browser Using GridView Row Command In ASP.NET C#
i am trying to open PDF file in new Tab in browser, but its open same Tab.. iam using gridview Template field to open pdf.. 我正在尝试在浏览器的新标签页中打开PDF文件,但它会打开相同的标签页。IAM使用gridview模板字段打开pdf。
How To Open PDF Files In New Tab In Browser Using GridView Row Command In ASP.NET C# 如何在ASP.NET C#中使用GridView行命令在浏览器的新选项卡中打开PDF文件
ASP.NET ASP.NET
<asp:GridView ID="gvwPDF" runat="server" CssClass="mGrid" CellPadding="20" CellSpacing="20" AutoGenerateColumns="false" EmptyDataText="No files uploaded" Width="100%">
<Columns>
<asp:BoundField DataField="Text" HeaderText="File Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkRead" runat="server" Text="✉ Read" CommandName="Read" CssClass="gvwedit" ForeColor="Green" OnClick="ReadPDFFile" CommandArgument='<%# Eval("Value") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C# C#
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
gvwPDF.DataSource = files;
gvwPDF.DataBind();
}
}
catch (Exception ex)
{
//PopMsg.Pop(ex.Message.ToString(), BmGate.WebFormUserControls.Common.MessageBox.IconError, "Error");
ScriptManager.RegisterStartupScript(this, GetType(), "Error Message", "alert('" + ex.Message.ToString() + "')", true);
}
}
protected void ReadPDFFile(object sender, EventArgs e)
{
try
{
string path = (sender as LinkButton).CommandArgument;
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);
if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
ClientScript.RegisterClientScriptBlock(this.GetType(), "Message", "window.open('application/pdf','_blank');", true);
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, GetType(), "Error Message", "alert('" + ex.Message.ToString() + "')", true);
}
}
help to solve this issue.. 帮助解决这个问题。
In your LinkButton, set the OnClientClick to this: 在您的LinkButton中,将OnClientClick设置为此:
<asp:LinkButton ID="lnkRead" runat="server" Text="✉ Read" CommandName="Read" CssClass="gvwedit" ForeColor="Green" OnClientClick="window.open('newPage.aspx?fileName=<%# Eval("Value") %>', '_newtab');"></asp:LinkButton>
This will open a new tab with the PDF file name as a QueryString ( Other solutions for opening new tabs here ). 这将打开一个新标签页,其中PDF文件名为QueryString( 此处提供其他用于打开新标签页的解决方案 )。 What you would want to change in your current
Page_Load
is making the value of the ListItem
the file name so that you are not passing a file path over URL parameter. 您要在当前
Page_Load
中进行的更改是使ListItem
的值成为文件名,以便您不会通过URL参数传递文件路径。
In the Page_Load
of newPage.aspx
(the new tab being opened), load your pdf data. 在
newPage.aspx
的Page_Load
(正在打开的新选项卡)中,加载pdf数据。 If you are receiving a Byte[]
from your WebClient
, this is how I write the PDF: 如果您从
WebClient
收到Byte[]
,这就是我编写PDF的方式:
string fileName = Request.QueryString["fileName"];
string path = Path.Combine(Server.MapPath("~/Uploads/"), fileName);
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + fileName);
HttpContext.Current.Response.BinaryWrite(buffer);
HttpContext.Current.Response.End();
Let me know if this works for you. 让我知道这是否适合您。 I've tested the
OnClientClick
in the LinkButton
and it opened a new tab successfully. 我已经在
LinkButton
测试了OnClientClick
,它成功打开了一个新标签页。 Separately, I've used the Response
code above in a few different pages without issue. 另外,我在几个不同的页面中使用了上面的
Response
代码,没有出现问题。
I see you are already trying to set the target to _blank, but not until the PDF is being constructed, which is too late. 我看到您已经在尝试将目标设置为_blank,但是直到构造PDF为止,这已经太晚了。 It has to be done in the page that has the LinkButtons.
必须在具有LinkButtons的页面中完成。
Try using the GridView_RowDataBound
event of gvwPDF
to modify the LinkButtons, something like this: 尝试使用
gvwPDF
的GridView_RowDataBound
事件来修改LinkButton,如下所示:
var linkButton = e.Row.FindControl("lnkRead");
linkButton.Attributes.Add("target", "_blank");
尝试添加内容处置标头:
Response.AddHeader("content-disposition", "attachment;filename=sample1.pdf");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.