Fetch and display HTML from Database in IFRAME using jQuery and Ajax

suhaas121
 
on Sep 19, 2021 10:59 PM
1624 Views

Hi

i want to fetch html page from db using ajax web method. I am able to fetch the page but only some part of the page is displayed but in the folder i have the full page. Please help.

$('#frmDisplay').on('load', function () {
    $('#frmDisplay').contents().find('a.anchorLink').click(function () {
        var id = $(this).attr('id');
      <%--  var hid = document.getElementById('<%= HiddenField6.ClientID %>');
        hid.value = id;--%>
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Amm.aspx/getlink",
            data: "{'Id': '" + id + "'}",
            dataType: "json",
            success: function (data) {
                $("#frmDisplay").attr('src', "data:text/html;charset=utf-8," + encodeURI(data.d));
                
            },
            error: function (response) {
                alert(response.responseText);
            }
        });
    });
});

 

public static string getlink(int Id)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connString"].ConnectionString);
    string link = "extlink";
    BookTree obj = new BookTree();
    DataSet ds = obj.getlink(Id);
    SqlCommand cmd=new SqlCommand("select vcFilePath from tblBookNodes where iModuleId='" + Id + "'",conn);
    conn.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        bytes = (byte[])dr["vcFilePath"];
    }
    string fileName = link.Replace(" ", "_") + ".htm";
    // DirectoryInfo strPath = new DirectoryInfo(HttpContext.Current.Server.MapPath(@"~/Linking/"));
    //string strPath = HttpContext.Current.Server.MapPath(@"/Linking/") + fileName;
    //foreach (FileInfo file in strPath.GetFiles())
    //{
    //    file.Delete();
    //}
    string path = Path.Combine(HttpContext.Current.Server.MapPath("~/Linking/"), fileName);
    var doc = new HtmlDocument();
    string html = Encoding.UTF8.GetString(bytes);
    doc.LoadHtml(html);
    StringWriter sw = new StringWriter();
    var hw = new HtmlTextWriter(sw);
    StreamWriter sWriter = new StreamWriter(path);
    sWriter.Write(sw.ToString());
    doc.Save(sWriter);
    sWriter.Close();
    //string fileContents = html;
    //System.IO.File.WriteAllText(path, fileContents);
    return html ;
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 22, 2021 07:26 AM
suhaas121 says:
$("#frmDisplay").attr('src', "data:text/html;charset=utf-8," + encodeURI(data.d));

You are returning html string.

Set the iframe content html instead of scr.

$('#frmDisplay').contents().find('html').html(data.d)

Or use below.

$("#frmDisplay").attr('src', "data:text/html;charset=utf-8," + escape(data.d));