using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Masters_EmployeeDocumentView_Popup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string filePath = string.Empty;
        string returnData = string.Empty;
        filePath = Request.QueryString["Filepath"].ToString();
        //iframePDf.Disposed;
        //iframePDf.Attributes["Src"] = filePath;
        if (File.Exists(filePath))
        {
            LoadPDF(filePath);
            returnData = "Success";
        }
        else
        {
            returnData = "unSuccess";
            Response.Clear();
            Response.ContentType = "application/json";
            Response.Write("{\"data\": \"" + returnData + "\"}");
            Response.End();
        }
    }

    private void LoadPDF(string filePath)
    {
        // Assuming FilePath is the path to your PDF file
        //string FilePath = Server.MapPath(filePath);
        if (File.Exists(filePath))
        {
            byte[] FileBuffer = System.IO.File.ReadAllBytes(filePath);

            if (FileBuffer != null)
            {
                Response.Clear();
                // Set the content type to PDF
                Response.ContentType = "application/pdf";
                // Set the content length
                Response.AddHeader("content-length", FileBuffer.Length.ToString());
                // Write the PDF binary data to the response
                Response.BinaryWrite(FileBuffer);
                // Embed the PDF within the iframe
                Response.Flush(); // Flush the response to ensure all data is sent
                string base64Data = Convert.ToBase64String(FileBuffer);

                iframePDf.Attributes["src"] = "data:application/pdf;base64," + base64Data;
                hdnFilepath.Value = filePath;
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('There is no Record');", true);

        }

    }
}