English 中文(简体)
如何使用asp.net c#在面板或iframe中打开.pdf文件。
原标题:how to open a .pdf file in a panel or iframe using asp.net c#
  • 时间:2010-02-08 12:18:12
  •  标签:
  • c#
  • asp.net

我正在尝试在按钮单击时打开一个 .pdf 文件。我想将一个 .pdf 文件打开到一个面板或某个 iframe 中。使用以下代码,我只能在单独的窗口或保存模式中打开 .pdf 文件。

string filepath = Server.MapPath("News.pdf");
    FileInfo file = new FileInfo(filepath);
    if (file.Exists)
    {
        Response.ClearContent();            
        Response.AddHeader("Content-Disposition", "inline; filename=" + file.Name); 
        Response.AddHeader("Content-Length", file.Length.ToString());            
        Response.ContentType = ReturnExtension(file.Extension.ToLower());            
        Response.TransmitFile(file.FullName);
        Response.End();
    }

如何将 iframe 指派给下面这行代码。

Response.AddHeader("Content-Disposition", "inline; filename=" + file.Name); 
问题回答

我很抱歉,我可以直接回答你的问题(无论听到用回应头来说明一个框架)。

相反,如果你将iFrame的斜体放到向客户撰写人民抵抗力量的网页/url。 这样,服务器只能关注数据发送,客户选择在哪里显示数据。

在.aspx页面中

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:LinkButton ID="lnkBtnPDFViewer" runat="server" OnClick="lnkBtnPDFViewer_Click" ToolTip="View PDF Document" CssClass="btn btn-white btn-sm"><span class="fa fa-file-pdf-o"></span></asp:LinkButton>
 <asp:Button ID="btnDocShow" runat="server" CssClass="hidden" />
        <!-- ModalPopupExtender -->
        <cc1:ModalPopupExtender ID="mpeDocViewer" runat="server" PopupControlID="pnlDocViewer" TargetControlID="btnDocShow"
            CancelControlID="btnDocClose" />
        <asp:Panel ID="pnlDocViewer" runat="server" CssClass="ibox" Style="display: none;">
            <div class="ibox-title">
                <h5>Employee Doc Viewer</h5>
                <div class="ibox-tools">
                    <a class="close-link">
                        <i id="btnDocClose" class="fa fa-times"></i>
                    </a>
                </div>
            </div>
            <div class="ibox-content">
                <iframe src="#" id="iframePDFViewer" width="800" height="500"  runat="server"></iframe>
            </div>
        </asp:Panel>

相应服务器侧代码

protected void lnkBtnPDFViewer_Click(object sender, EventArgs e)
    {
        string filePath = Request.Url.Scheme + "://" + Request.Url.Authority + myfile.pdf;
        iframePDFViewer.Src = filePath;
        mpeDocViewer.Show();
    }

这将在使用iFrame的配电式波中打开PDF文档。

参考:Code Project: Asp .Net PDF Viewer

代码格式化如下

private void ReadPdfFile()
{
    string path = @"D:Hemanthsample.pdf";
    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);
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签