'how to open a .pdf file in a panel or iframe using asp.net c#
I am trying to open a .pdf file on a button click. I want to open a .pdf file into a panel or some iframe. With the following code i can only open .pdf file in a separate window or in a save as mode.
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();
}
how to assign a iframe to the below line
Response.AddHeader("Content-Disposition", "inline; filename=" + file.Name);
Solution 1:[1]
I'm sorry, I can't answer your question directly (never heard of specifying a frame using a response header.)
What if, instead, you set the src of your iFrame to the page/url that writes the PDF to the client. That way the server only cares about sending back the data and the client is choosing where to display it.
Solution 2:[2]
In .aspx page
<%@ 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>
In the respective server side code
protected void lnkBtnPDFViewer_Click(object sender, EventArgs e)
{
string filePath = Request.Url.Scheme + "://" + Request.Url.Authority + myfile.pdf;
iframePDFViewer.Src = filePath;
mpeDocViewer.Show();
}
This will open the PDF file in a modal popup using iFrame.
Reference: Code Project: Asp .Net PDF Viewer
Solution 3:[3]
Code formated for the below
private void ReadPdfFile()
{
string path = @"D:\Hemanth\sample.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);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kevin Buchan |
| Solution 2 | Pranesh Janarthanan |
| Solution 3 | Pranesh Janarthanan |
