0

Hi i want to display html page from DB using web method on anchor tag click. I get html from db but not able to display inside iframe. The html page is stored in binary format which i will be converting back. I am not able to display html page when i try to pass html through string. 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.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 fileContents.ToString().Trim('\n' , '\r' , '\t') ;
        } 

2 Answers 2

0

As per your code, you want to display HTML content in iframe src instead of URL.

try following way.

<iframe src="data:text/html;charset=utf-8,%3Chtml%3E%3Cbody%3Efoo%3C/body%3E%3C/html%3E" />
Sign up to request clarification or add additional context in comments.

8 Comments

I am getting bad request error . imgur.com/zf6lfQx
In your ajax response you have the following line. $("#frmDisplay").attr('src', data.d); So data.d should be your HTML content. and start with data:text/html;charset=utf-8,
when i change content in ajax to text/html i get something like this imgur.com/xkv1Yjt . Html page is not getting displayed
Can you please try with encodeURI(). E.g. $("#frmDisplay").attr('src', data:text/html;charset=utf-8,encodeURI(data.d));
should i use this in success method ? because its showing syntax error
|
0

In my own case, although not in .NET, I was able to render HTML returned from an api using srcdoc attribute of an <iframe> as follows:

<iframe srcdoc="<html><body>Hello, <b>world</b>.</body></html>"></iframe>

source: documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.