PDF File not opening in modal popup in asp.net using embed

BeUnique 2,112 Reputation points
2024-06-06T13:53:41.8333333+00:00

I am unable to open the pdf file which is located in file server path through modal popup.

pls. find below code.

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<div class="modal fade" id="PopModal" role="dialog">
     <div class="modal-dialog">
         <!-- Modal content-->
         <div class="modal-content">
             <div class="modal-header">
                 <button type="button" class="close" data-dismiss="modal">
                     &times;</button>
                 <h4 class="modal-title">Customer Details</h4>
             </div>
             <div class="modal-body">
                 <div class="col-lg-12 col-sm-12 col-md-12 col-xs-12">
                     <div class="form-group">
                     </div>
                     <div class="form-group">
                        <asp:Literal ID="ltEmbed" runat="server" />
                     </div>
                 </div>
             </div>
             <div class="modal-footer">
                 <asp:Button ID="btnDownload" runat="server" Text="Download" OnClick="DownloadFile" />
                 <button type="button" data-dismiss="modal">Close</button>
             </div>
         </div>
     </div>
 </div>
protected void Display(object sender, EventArgs e)
 {
     int rowIndex = Convert.ToInt32(((sender as LinkButton).NamingContainer as GridViewRow).RowIndex);
     GridViewRow row = gvCustomers.Rows[rowIndex];
     string FPath = (row.FindControl("HiddenField1") as HiddenField).Value;
     Session["FPath"] = FPath;
     if (!string.IsNullOrEmpty(FPath))
     {
         string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"500px\" height=\"300px\">";
         embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";
         embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
         embed += "</object>";
         ltEmbed.Text = string.Format(embed, ResolveUrl(FPath));              
         ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "openModal();", true);
     }
 }

I tested with correct path. but, still pdf not loading in the modal popup.

path is correct and checked multiple times.

below is the debugging screenshot.

User's image

But, the same path, i am able to open image file through modal popup.

what is the problem and how we could identify this...?

kindly help to identify the exact problem....

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,410 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,623 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 28,831 Reputation points Microsoft Vendor
    2024-06-07T02:14:21.6233333+00:00

    Hi @Gani_tpt,

    It's possible that the file cannot be accessed for browser security reasons.

    Perhaps you can change it to the same implementation as for images, and view the PDF by getting the bytes.

     <iframe id="iframe" runat="server" width="500" height="500"></iframe>
    
     protected void Display(object sender, EventArgs e)
     {
         int rowIndex = Convert.ToInt32(((sender as LinkButton).NamingContainer as GridViewRow).RowIndex);
         GridViewRow row = gvCustomers.Rows[rowIndex];
         string FPath = (row.FindControl("HiddenField1") as HiddenField).Value; 
         string sMineType = MimeMapping.GetMimeMapping(FPath);
         if (!string.IsNullOrEmpty(FPath))
         {                        
                 byte[] rawFile = File.ReadAllBytes(FPath);
                 iframe.Attributes["src"] =$@"data:{sMineType};base64,{Convert.ToBase64String(rawFile)}";
                 ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "openModal();", true);             
         }
        
     }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,181 Reputation points
    2024-06-06T15:15:23.0266667+00:00

    Your data is not a valid url. Looks like you are trying to use a file share which browsers don’t support.

    0 comments No comments

  2. AgaveJoe 27,656 Reputation points
    2024-06-06T15:27:17.6+00:00

    Browser security does not allow accessing local resource like a network share. Frankly, it is not clear if the the application users users can access the network share either. Keep in mind, browser security has been explained to you several times... Moving on...

    In your previous thread(s) we discussed several approaches. One was taking advantage of ASP.NET generic handler to return a file stream. We also provided a link to source code that illustrates how to return a file.

    Generic Handler Example (from the information already provided)

        public class GenericFileHandler : IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
                string filename = string.Empty;
                if (context.Request.QueryString["filename"] != null) 
                {
                    filename = context.Request.QueryString["filename"].ToString();
                }
                System.IO.FileInfo fileInfo = new System.IO.FileInfo($@"\\net.m.intranet\AdobeFiles\{filename}");
                if (fileInfo.Exists)
                {
                    context.Response.Clear();
                    context.Response.AddHeader("Content-Disposition", "inline;attachment; filename=\"" + fileInfo.Name + "\"");
                    context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                    context.Response.ContentType = "application/pdf";
                    context.Response.TransmitFile(fileInfo.FullName);
                    context.Response.Flush();
                }
                else
                {
                    throw new Exception("File not found");
                }
            }
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    

    Example link to the file.

    <a href="GenericFileHandler.ashx?filename=CLARIFICATIONS.pdf">Example Link</a>
    
    

    You can use the href to test the PDF download works. Keep in mind, the web application identity needs read access to the network share. If you can't download the PDF from the link then it could be a permissions issue.

    Once verified, use the href URL in the object or iframe element.