我正在尝试在浏览器的新标签页面中打开PDF文件,但是它打开了相同的标签.. 我使用gridview Template字段打开pdf ..
如何在ASP.NET C#中使用GridView Row命令在浏览器的新选项卡中打开PDF文件
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#
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);
}
}
帮助解决这个问题..
答案 0 :(得分:3)
在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>
这将打开一个新的选项卡,其PDF文件名为QueryString(Other solutions for opening new tabs here)。您希望在当前Page_Load
中更改的是使ListItem
文件名的值,以便您不通过URL参数传递文件路径。
在Page_Load
newPage.aspx
(正在打开的新标签页)中,加载您的pdf数据。如果您从Byte[]
收到WebClient
,这就是我写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();
如果这对您有用,请告诉我。我测试了OnClientClick
中的LinkButton
,并成功打开了新标签页。另外,我在几个不同的页面中使用了上面的Response
代码而没有问题。
答案 1 :(得分:0)
我发现你已经在尝试将目标设置为_blank,但是在构建PDF之前,这已经太晚了。它必须在具有LinkButtons的页面中完成。
尝试使用GridView_RowDataBound
gvwPDF
事件来修改LinkButtons,如下所示:
var linkButton = e.Row.FindControl("lnkRead");
linkButton.Attributes.Add("target", "_blank");
答案 2 :(得分:-1)
尝试添加内容处置标头:
{{1}}