[Solved] ASP.Net Error: Illegal characters in path

suhaas121
 
on Aug 29, 2021 11:17 AM
1661 Views

hi

i want to fetch and display html page from db on click in a html page from ajax web method. Not able to display html page in a iframe and multiview.

Sample HTML

<html><head><title>DMC-DO228-AAA-00-00-00-01A-000A-A.htm</title>
</head><body>
<p> just to check </p>
<a id="5" class="anchorLink" href="#">just to check</a>
<a id="6" class="anchorLink" href="#">just to check</a>
</body>
</html>

I am getting error "illegall characters in path" when I read htm as a string.

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/"));
    //foreach (FileInfo file in strPath.GetFiles())
    //{
    //    file.Delete();
    //}
    string path = strPath + 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(html);
    sWriter.Write(sw.ToString());
    doc.Save(sWriter);
    sWriter.Close();
    string fileContents = html;
    System.IO.File.WriteAllText(html, fileContents);
    return fileContents;
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 16, 2021 06:47 AM
suhaas121 says:
            DirectoryInfo strPath = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/Linking/"));
            //foreach (FileInfo file in strPath.GetFiles())
            //{
            //    file.Delete();
            //}
            string path = strPath + fileName;

Here is the issue.

You are concatenating DirectoryInfo with file name.

string fileName = link.Replace(" ", "_") + ".htm";
string path = Path.Combine(HttpContext.Current.Server.MapPath("~/Linking/"), fileName);

Path should be like above.