【问题标题】:Return html page in webmethod inside iframe multiview c#在iframe multiview c#中的webmethod中返回html页面
【发布时间】:2021-11-12 21:21:05
【问题描述】:

您好,我想在单击锚标记时使用 Web 方法显示来自 DB 的 html 页面。我从 db 获取 html,但无法在 iframe 中显示。 html页面以二进制格式存储,我将转换回来。当我尝试通过字符串传递 html 时,我无法显示 html 页面。请帮忙

 $('#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') ;
        } 

【问题讨论】:

    标签: c# asp.net ajax webmethod


    【解决方案1】:

    根据您的代码,您希望在 iframe src 而不是 URL 中显示 HTML 内容。

    尝试以下方式。

    <iframe src="data:text/html;charset=utf-8,%3Chtml%3E%3Cbody%3Efoo%3C/body%3E%3C/html%3E" />
    

    【讨论】:

    • 我收到错误的请求错误。 imgur.com/zf6lfQx
    • 在您的 ajax 响应中,您有以下行。 $("#frmDisplay").attr('src', data.d);所以 data.d 应该是你的 HTML 内容。并从 data:text/html;charset=utf-8, 开始
    • 当我将 ajax 中的内容更改为 text/html 时,我会得到类似 imgur.com/xkv1Yjt 的内容。没有显示 Html 页面
    • 你可以试试 encodeURI()。 E.g. $("#frmDisplay").attr('src', data:text/html;charset=utf-8,encodeURI(data.d));
    • 我应该在成功方法中使用这个吗?因为它显示语法错误
    猜你喜欢
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多