Issue to PDF file

Peter_1985 2,866 Reputation points
2023-04-04T04:37:00.2133333+00:00

Hi, Is there one example to load/open one PDF file within the web page?

Developer technologies | ASP.NET | ASP.NET API
Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Lan Huang-MSFT 30,206 Reputation points Microsoft External Staff
    2023-04-04T06:40:07.2233333+00:00

    Hi @Peter_1985,
    Hope the following two examples will help you.

    <asp:FileUpload ID="fuUpload" runat="server" />
    <asp:Button Text="Submit" OnClick="OnSubmit" runat="server" />
    <hr />
    <asp:Literal ID="ltEmbed" runat="server" />
    
     protected void OnSubmit(object sender, EventArgs e)
            {
                ltEmbed.Visible = true;
                if (!System.IO.Directory.Exists(Server.MapPath("~/Files")))
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath("~/Files"));
                }
                fuUpload.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Files"), System.IO.Path.GetFileName(fuUpload.PostedFile.FileName)));
                string file = "~/Files/" + System.IO.Path.GetFileName(fuUpload.PostedFile.FileName);
                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(file));
            }
    

    13

     <div>  
            <asp:Button ID="bttnpdf" runat="server" Text="Click for open PDF" Font-Bold="True" OnClick="bttnpdf_Click" />  
        </div>  
    
    protected void bttnpdf_Click(object sender, EventArgs e)  
        {  
            string FilePath = Server.MapPath("javascript1-sample.pdf");  
            WebClient User = new WebClient();  
            Byte[] FileBuffer = User.DownloadData(FilePath);  
            if (FileBuffer != null)  
            {  
                Response.ContentType = "application/pdf";  
                Response.AddHeader("content-length", FileBuffer.Length.ToString());  
                Response.BinaryWrite(FileBuffer);  
            }  
        }  
    

    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 additional answer

Sort by: Most helpful
  1. Jack1 0 Reputation points
    2023-04-12T06:45:32.92+00:00

    I got my answer thank you.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.