如何在新标签页中打开pdf [英] How to open pdf in new tab
本文介绍了如何在新标签页中打开pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在gridview中有一个LinkButton。
我想在新标签中打开数据库中的pdf文件。
在我的代码中,pdf在同一个窗口中打开而不是在新的选项卡。
我的尝试:
I have a LinkButton in gridview.
I want to open pdf file from database in new tab.
In my code pdf is opening in same window not in new tab.
What I have tried:
<asp:TemplateField HeaderText="View File">
<ItemTemplate>
<asp:LinkButton ID="lbtnViewFile" runat="server" CommandArgument='<%#System.Web.HttpUtility.HtmlEncode(Eval("AppCode") + "," + Eval("AttachmentCode") + "," + Eval("AttachmentCodeSerialNumber"))%>' OnCommand="lbtnViewFile_Click">View</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
protected void lbtnViewFile_Click(object sender, CommandEventArgs e)
{
LinkButton btn = (LinkButton)sender;
string[] CommandArgument = btn.CommandArgument.Split(',');
long lng_AppCode = Convert.ToInt32(CommandArgument[0]);
int int_attachmentCode = Convert.ToInt32(CommandArgument[1]);
int int_attachmentCodeSerialNumber = Convert.ToInt32(CommandArgument[2]);
DownloadFiles(lng_AppCode,
int_attachmentCode, int_attachmentCodeSerialNumber);
}
Private int DownloadFiles(long lng_AppCode, int int_attachmentCode, int int_attachmentCodeSerialNumber)
{
try
{
int intResult = 0;
ABC obj_ABC = new ABC();
A obj_A =
obj_ABC.DownloadFile(lng_AppCode,
int_attachmentCode, int_attachmentCodeSerialNumber);
if (obj_A != null)
{
byte[] bytes = obj_A.AttachmentFile;
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = obj_A.ContentType;
HttpContext.Current.Response.BinaryWrite(bytes);
if (obj_A.ContentType == "application/pdf")
{
HttpContext.Current.Response.AddHeader("content-disposition", "inline;filename=" + "file_" + Convert.ToString(lng_AppCode) + "_" + Convert.ToString(int_attachmentCode) + "_" + Convert.ToString(int_attachmentCodeSerialNumber) + obj_A.FileExtension);
}
else
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + "file_" + Convert.ToString(lng_AppCode) + "_" + Convert.ToString(int_attachmentCode) + "_" + Convert.ToString(int_attachmentCodeSerialNumber) + obj_A.FileExtension);
HttpContext.Current.Response.Flush();
intResult = 1;
}
return intResult;
}
catch (Exception)
{
return 0;
}
}
推荐答案
Google是你的朋友:好好经常拜访他。他可以比在这里发布问题更快地回答问题...
使用你的主题非常快速的搜索,因为大部分搜索词都给出了近百万次点击,包括这里和SO代码:在新标签页中打开来自数据库的pdf文件C# - Google搜索 [ ^ ]
将来,请尝试自己做至少基础研究,不要浪费你的时间或我们的时间。
Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...
A very quick search using your subject as most of the search term gave nearly a million hits, including here and SO with code: open pdf file from database in new tab C# - Google Search[^]
In future, please try to do at least basic research yourself, and not waste your time or ours.
这篇关于如何在新标签页中打开pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文